How to Bind Data to ListBox Control using DataTable in ASP.NET C# VB.Net

Asp.Net ListBox 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 ListBox control. From this article you will also learn how one can generate runtime DataTable and Bind it to the ListBox Control.

Output/Outcome:
How to Bind Data to ListBox Control using DataTable in ASP.NET Csharp VB.Net

Now need to add a ListBox Control:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>
</asp:Content>

Now we need to Bind Data into ListBox 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");

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

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

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

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

            ListBox1.DataSource = dt;
            ListBox1.DataTextField = "Name";
            ListBox1.DataValueField = "ID";
            ListBox1.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")

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

            oItem = dt.NewRow()
            oItem(0) = "2000"
            oItem(1) = "Bimalendu"
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "3000"
            oItem(1) = "Purnendu"
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "4000"
            oItem(1) = "Roger"
            dt.Rows.Add(oItem)

            ListBox1.DataSource = dt
            ListBox1.DataTextField = "Name"
            ListBox1.DataValueField = "ID"
            ListBox1.DataBind()
        End If

    End Sub

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

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

Download Code Example C#        Download Code Example VB.Net

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

Leave a Reply

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

     

*