Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2004, 08:48 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default Binding Data to a DataGrid

Hi There,

I have the problem of in my datagrid i have named the columns and set where i want the data to be displayed... but it displays all the information twice once under my headings and the second time under the database field names. have i set something wrong, i tried working it out with the beginners guide but i cant seem to work it out.

<%@ Page Language="VB" %>
<script runat="server">

    'This binds the data from the messages to the datagrid

    Sub Page_Load()

        GridMessages.DataSource = GetMessages
        GridMessages.DataBind()

    End Sub


        'This gets all the message details from the database

        Function GetMessages() As System.Data.DataSet
            Dim connectionString As String = ConfigurationSettings.AppSettings("connectionStrin g")
            Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )

            Dim queryString As String = "SELECT [Messages].[MessageID], [Messages].[GuestName], [Messages].[GuestEmail], ["& _
    "Messages].[GuestURL], [Messages].[GuestMessage] FROM [Messages]"
            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection

            Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
            dataAdapter.SelectCommand = dbCommand
            Dim dataSet As System.Data.DataSet = New System.Data.DataSet
            dataAdapter.Fill(dataSet)

            Return dataSet
        End Function

</script>
<html>
<head>
    <link href="includes/stylesheet.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <form runat="server">
        <p align="center">
            <img height="60" src="images/logo.jpg" width="468" />
        </p>
        <p align="center">
            Sign Guest Book | View Guest Book
        </p>
        <p align="center">
            <table cellspacing="0" cellpadding="0" width="600" border="0">
                <tbody>
                    <tr>
                        <td width="600">
                            <asp:DataGrid id="GridMessages" runat="server" CssClass="GridMessages" Width="827px" AllowPaging="True" CellSpacing="-1" BorderWidth="1px" BorderColor="Black" CellPadding="2">
                                <FooterStyle borderwidth="1px" borderstyle="Solid" bordercolor="Black" backcolor="Blue"></FooterStyle>
                                <HeaderStyle horizontalalign="Left" borderwidth="1px" borderstyle="Solid" bordercolor="Black" verticalalign="Top" backcolor="DarkGray"></HeaderStyle>
                                <AlternatingItemStyle borderwidth="1px" borderstyle="Solid" bordercolor="Black" backcolor="#E0E0E0"></AlternatingItemStyle>
                                <Columns>
                                    <asp:TemplateColumn HeaderText="Name">
                                        <ItemTemplate>
                                            <%# Container.DataItem("GuestName") %>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="Email">
                                        <ItemTemplate>
                                            <%# Container.DataItem("GuestURL") %>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="Homepage">
                                        <ItemTemplate>
                                            <%# Container.DataItem("GuestEmail") %>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="Message">
                                        <ItemTemplate>
                                            <%# Container.DataItem("GuestMessage") %>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                </Columns>
                            </asp:DataGrid>
                        </td>
                    </tr>
                </tbody>
            </table>
        </p>
    </form>
</body>
</html>


Cheers



David
__________________
David Jenkins
 
Old August 8th, 2004, 09:32 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to add AutoGenerateColumns="False" to the datagrid markup.

                            <asp:DataGrid id="GridMessages" runat="server" CssClass="GridMessages" Width="827px" AllowPaging="True" CellSpacing="-1" BorderWidth="1px" BorderColor="Black" CellPadding="2" AutoGenerateColumns="False">
 
Old August 8th, 2004, 10:14 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default

Hi there,

Thanks for that! I wanted to specify the width of each column and the valign of each of each column. I have tried putting these into various parts of the code but it generates errors. Is it possible?

Cheers for you help.

David
 
Old August 9th, 2004, 09:05 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Within the column definitions (e.g.: <asp:boundcolumn...) you can specify the width:

itemstyle-width="100"

However as is always the case with HTML, explicit widths on table cells can be problematic.
 
Old August 9th, 2004, 11:47 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default

its an <asp:itemtemplate> column and it says that width is not an attribute for it. i tried it in the <itemtemplate> tag as well with no results. *sigh*

David
 
Old August 9th, 2004, 02:27 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

it's not "width", it's "itemstyle-width". (original reply of mine was wrapped at the "-")





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with binding data in Datagrid Samatha ASP.NET 1.0 and 1.1 Professional 6 December 6th, 2006 09:13 AM
Data Binding Using DataGrid hoailing22 ASP.NET 1.0 and 1.1 Basics 17 November 14th, 2006 02:17 AM
Datagrid binding two dropdown inkrajesh ASP.NET 1.0 and 1.1 Basics 0 July 18th, 2006 10:46 PM
DataGrid Binding GrindCrusher Classic ASP Databases 0 February 21st, 2004 05:52 PM
Binding an aarraylist to a datagrid Delano ADO.NET 2 September 22nd, 2003 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.