Bind ListControl's DataValueField/TextField to multiple columns

 Rate It (1)

A very common question asked on the ASP.NET Forums is how to bind a ListControl's DataTextField or DataValueField to multiple Columns. The ListControl is the base class for controls such as DropDownList, ListBox, RadioButtonList, and CheckBoxList server controls.

One of the ways to do this task will be presented in this Wiki article. The code is shown in both VB.NET and C#.NET.

To start with, let us add a ListBox on an ASP.NET Page as follows:

<asp:ListBox ID="lstVendors" runat="server" />

 

Then, we will add a SqlDataSource to retrieve the data from the database. The database used is the AdventureWorks DB and the table used is the Purchasing.Vendor table.

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
        SelectCommand="SELECT TOP 5 [ActiveFlag], [VendorID], [Name] FROM [Purchasing].[Vendor]" />

 The UI is not setup, let us move to the code behind. Before doing so, notice that the ListBox doesn't specify a DataSource. This is done intentionally because the binding will be done manually in the code behind. In the page_load method of the ASP.NET page, let us add the following code:

C#.NET
----------- 
    protected void Page_Load(object sender, EventArgs e)
    {
        // Configure the Listbox only once, the first time
        if (!Page.IsPostBack)
        {          
            // Manually execute the SqlDataSource Select method.
            // The return value is an IEnumerable and the real return
            // value is a DataView
            DataView rows = (DataView)this.SqlDataSource1.Select(new DataSourceSelectArguments());
           
            // If there is data in the DataView
            if ((rows != null) && (rows.Count >= 1))
            {
                // Loop through the record
                for (int i = 0; i < rows.Count; i++)
                {
                    // Create a new ListItem with the:
                    // Text: VendorID
                    // Value: Name;ActiveFlag
                    ListItem li = new ListItem(
                        rows[i]["VendorID"].ToString(),
                        string.Concat(rows[i]["Name"].ToString(), ";", rows[i]["ActiveFlag"].ToString()
                        ));

                    // Add the ListItem to the lstVendors
                    this.lstVendors.Items.Add(li);
                }
            }
        }
    }

VB.NET
-----------
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  ' Configure the Listbox only once, the first time
  If (Not Page.IsPostBack) Then
   ' Manually execute the SqlDataSource Select method.
   ' The return value is an IEnumerable and the real return
   ' value is a DataView
   Dim rows As DataView = CType(Me.SqlDataSource1.Select(New DataSourceSelectArguments()), DataView)

   ' If there is data in the DataView
   If (Not rows Is Nothing) AndAlso (rows.Count >= 1) Then
    ' Loop through the record
    For i As Integer = 0 To rows.Count - 1
     ' Create a new ListItem with the:
     ' Text: VendorID
     ' Value: Name;ActiveFlag
     Dim li As ListItem = New ListItem(rows(i)("VendorID").ToString(), String.Concat(rows(i)("Name").ToString(), ";",

rows(i)("ActiveFlag").ToString()))

     ' Add the ListItem to the lstVendors
     Me.lstVendors.Items.Add(li)
    Next i
   End If
  End If
 End Sub

Hope this article helps you out satisfy the requirement of binding the DataTextField or DataValueField of ListControls to multiple columns.

Regards

Revision number 1, Sunday, February 17, 2008 8:24:45 AM by
This is not the most up to date version of this article. The most recent version can be found here.

Comments

Related Articles

Web Controls

Web controls are ASP.NET controls which are understood and compiled on the server and render as HTML markup on the page. Most of the web server controls render their own HTML controls (or many HTML controls). The markup that is output froma web control can

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter