Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example shows how to bind a DataTable in a DataList control. Just for the simplicity of this demo I’m going to create a dummy data in the DataTable. See the code blocks below:

 

private DataTable GetData()

{

        DataTable dt = new DataTable();

        DataRow dr;

        dt.Columns.Add(new System.Data.DataColumn("Column1", typeof(String)));

        dt.Columns.Add(new System.Data.DataColumn("Column2", typeof(String)));

        dt.Columns.Add(new System.Data.DataColumn("Column3", typeof(String)));

 

        dr = dt.NewRow();

        dr[0] = "Column1 Item A";

        dr[1] = "Column2 Item B";

        dr[2] = "Column3 Item C";

        dt.Rows.Add(dr);

 

        dr = dt.NewRow();

        dr[0] = "Column1 Item AA";

        dr[1] = "Column2 Item BB";

        dr[2] = "Column3 Item CC";

        dt.Rows.Add(dr);

 

        dr = dt.NewRow();

        dr[0] = "Column1 Item AAA";

        dr[1] = "Column2 Item BBB";

        dr[2] = "Column3 Item CCC";

        dt.Rows.Add(dr);

 

        dr = dt.NewRow();

        dr[0] = "Column1 Item AAAA";

        dr[1] = "Column2 Item BBBB";

        dr[2] = "Column3 Item CCCC";

        dt.Rows.Add(dr);

 

        return dt;

}

 

Now let’s set up the DataList in the ASPX source like below.

 

<asp:DataList ID="DataList1" runat="server">

        <HeaderTemplate>

               <table>

                    <tr>

                    <th style="width:150px" align="left">HEADER 1</th>

                    <th style="width:150px" align="left">HEADER 2</th>

                    <th style="width:150px" align="left">HEADER 3</th>

                    </tr>

                </table>

        </HeaderTemplate>

        <ItemTemplate>

                <table>

                    <tr>

                     <td style="width:150px" align="left"> <asp:Label ID="LabelTest1" Text='<%# DataBinder.Eval(Container.DataItem, "Column1") %>' runat="server" /></td>

                     <td style="width:150px" align="left"> <asp:Label ID="LabelTest2" Text='<%# DataBinder.Eval(Container.DataItem, "Column2") %>' runat="server" /></td>

                     <td style="width:150px" align="left"> <asp:Label ID="LabelTest3" Text='<%# DataBinder.Eval(Container.DataItem, "Column3") %>' runat="server" /></td>

                    </tr>

                </table>

        </ItemTemplate>

</asp:DataList>

 

As you can see, the set up above was very simple. Now let’s go switch back to the code behind part of the page and bind the DataList with the data from the DataTable. Here’s the code block below:

 

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            DataTable dt = GetData();

            DataList1.DataSource = dt;

            DataList1.DataBind();

        }

    }

 

That’s Simple!

Technorati Tags: ,,
Posted on Thursday, August 13, 2009 10:41 PM ADO.NET , ASP.NET , C# | Back to top


Comments on this post: Bind DataTable to DataList Control

# re: Bind DataTable to DataList Control
Requesting Gravatar...
Hi vinz,

I am trying to bind the item to textbox but I can't seem to bind it. Just replace the label with the textbox.
Left by LadyBug on Mar 27, 2010 8:33 AM

# re: Bind DataTable to DataList Control
Requesting Gravatar...
Oh, it's not as difficult as it seems after all. Thanks for sharing.
Left by Girl sayings on Oct 22, 2010 2:33 AM

# re: Bind DataTable to DataList Control
Requesting Gravatar...
Yes, that was perfect!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Left by Hello on Aug 31, 2012 5:23 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net