Bind DataTable to DataList Control

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:,,

This article is part of the GWB Archives. Original Author: Vincent Maverick Durano

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.