How to Bind DataTble Data into Repeater Control in ASP.NET C# VB.Net

Asp.Net Repeater Control is one of the most useful control. We need to Bind data in the control in many cases. DataTable is one of the most popular DataSource and here in this article I will describe how to Bind DataTable Data into the Repeater Control. From this article you will also learn how one can generate runtime DataTable and Bind it to the Repeater Control.

Objective/Outcome:
How to Bind DataTble Data into Repeater Control in ASP.NET Csharp VB.Net

Now need to add a Repeater Control:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table border="1px" cellpadding="5" cellspacing="0" style="text-align:center;border-style:solid;border-collapse:collapse;">
        <tr style="background:CornflowerBlue;Color:White;">
            <th>Name</th>
            <th>Address</th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
            <td><%# Eval("Name")%></td>
            <td><%#Eval("Address")%></td>
        </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
        <tr style="background:#99CCFF;">
            <td><%# Eval("Name")%></td>
            <td><%#Eval("Address")%></td>
        </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
    </asp:Repeater>
</asp:Content>

Now we need to Bind Data into Repeater Control using DataTable:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("ID", System.Type.GetType("System.Int64"));
            dt.Columns.Add("Name");
            dt.Columns.Add("Address");

            DataRow oItem = dt.NewRow();
            oItem[0] = "1000";
            oItem[1] = "Shawpnendu";
            oItem[2] = "Dhaka, Bangladesh";
            dt.Rows.Add(oItem);

            oItem = dt.NewRow();
            oItem[0] = "2000";
            oItem[1] = "Bimalendu";
            oItem[2] = "Chittagonj, Bangladesh";
            dt.Rows.Add(oItem);

            oItem = dt.NewRow();
            oItem[0] = "3000";
            oItem[1] = "Purnendu";
            oItem[2] = "Noakhali, Bangladesh";
            dt.Rows.Add(oItem);

            oItem = dt.NewRow();
            oItem[0] = "4000";
            oItem[1] = "Roger";
            oItem[2] = "Newyork, USA";
            dt.Rows.Add(oItem);

            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }
    }

Note: Don’t forget to add “System.Data” namespace.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dt As New DataTable()

            dt.Columns.Add("ID", System.Type.[GetType]("System.Int64"))
            dt.Columns.Add("Name")
            dt.Columns.Add("Address")

            Dim oItem As DataRow = dt.NewRow()
            oItem(0) = "1000"
            oItem(1) = "Shawpnendu"
            oItem(2) = "Dhaka, Bangladesh"
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "2000"
            oItem(1) = "Bimalendu"
            oItem(2) = "Chittagonj, Bangladesh"
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "3000"
            oItem(1) = "Purnendu"
            oItem(2) = "Noakhali, Bangladesh"
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "4000"
            oItem(1) = "Roger"
            oItem(2) = "Newyork, USA"
            dt.Rows.Add(oItem)

            Repeater1.DataSource = dt
            Repeater1.DataBind()
        End If
    End Sub

Note: Don’t forget to add “System.Data” namespace.

Hope now you can bind Repeater to a DataTable from server side or code behind.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Repeater, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*