ASP.NET 2.0
Data Access
Data Controls
Integration
.NET Security
Communication
XML
IT Project Management
Articles > Data Controls > Sorting and Binding a DropDownList To a DataSet

Sorting and Binding a DropDownList To a DataSet

Written by Osamah Alabdullah

Introduction

This article explains a straight forward approach to bind a DropDownList to a DataSet. Furthermore, the article will show you how easily you can sort the elements contained in the DropDownList using a DataView.

Approach

For simplicity, we will use a stand alone countries XML file that has CountryName and CountryCode as XML elements. We will then read the xml file into a DataSet. Next will create a DataView and assign it the Default view of the previously created dataset. Next, we will do the magic.

The Code

The XML file used in this example is shown below for your reference.

Countries.xml

<?
xmlversion="1.0"encoding="utf-8"standalone="yes" ?>
<Countries>
 <Country>
    <CountryName>Kuwait</CountryName>
    <CountryCode>KW</CountryCode>
 </Country>
 <Country>
    <CountryName>United Kingdom</CountryName>
    <CountryCode>UK</CountryCode>
 </Country>
 <Country>
    <CountryName>United States</CountryName>
    <CountryCode>US</CountryCode>
 </Country>
</Countries>


Code Behind

In the page load event, we will check to see if the page has been posted back. We will bind the data only 1 time when the page is loaded initially. When the page is loaded for the first time, The Page Load event will call the BindCountries() method to populate and sort the DropDownList control.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCountries();
        }
    }
 
    protected void BindCountries()
    {
        DataSet _countries = new DataSet();
        _countries.ReadXml(Server.MapPath("Countries.xml")); //Read the XML file
        DataView _dv = _countries.Tables[0].DefaultView; // Create DataView
        dv.Sort = "CountryName"; // Sort by Country name
        drpdCountry.DataSource = _dv; // Set the DropDownList data source
        drpdCountry.DataTextField = "CountryName"; // Assign Text Field
        drpdCountry.DataValueField = "CountryCode"; //Assign value Field
        drpdCountry.DataBind(); // Bind the Data
        ListItem _listObj = new ListItem("Please select", ""); //Add the default value
        drpdCountry.Items.Insert(0, _listObj);
    }
 
    protected void drpdCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblSelection.Text = drpdCountry.SelectedItem.Text;
    }



HTML Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
 
    <form id="form1" runat="server">
    <div>
        <h1>
        Sorting and Binding A Drop Down List to a DataSet
    </h1>
    <hr />
    <asp:DropDownList ID="drpdCountry" runat="server" AutoPostBack="True"
            onselectedindexchanged="drpdCountry_SelectedIndexChanged">
    </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="lblSelection" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>


Considerations

This approach is quick but it might not be perfect. As the data size increases, this approach will become less efficient in terms of memory utilization as all data read from the XML file into the DataSet will be held in memory. This is very good in terms of the user experience and perhaps will save a few round trips to the server, but it will surely consume a lot of system memory.

If the amount of data is not big (e.g. country file) then this approach is perfect. However, if the data size is large, then using a forward only DataReader may be more efficient.

Happy Programming
This web site uses Kentico CMS, the content management system for ASP.NET developers.