SIGN UP MEMBER LOGIN:    
ARTICLE

Sorting in DropDown List in a simple way using Linq

Posted by Madhu K Articles | LINQ with C# November 12, 2010
In this post I am going to tell how you can sort the dropdown list based on DataTextField and DataValueField using Linq.
Reader Level:
Download Files:
 

Sorting dropdown list can be easily done using Linq. In this post I am going to tell how you can sort the dropdown list based on DataTextField and DataValueField using Linq.

Source for dropdown list:

<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList

DataBase table has columns SortID,Name and Country with following values:

1.gif

Binding the dropdown list with the datasource

private void populateDataText()
    {
        using (var db = cnn.ConDB())
        {
            var select = from table in db.SortTables
                         select table;
DropDownList1.DataSource = select.OrderBy(item=>item.SortID);
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "SortID";
            DropDownList1.DataBind();
        }
    }


In the above code the dropdown list is sorted based on the SortID, when we call the populateDropDown()the dropdown list looks like

2.gif

If you want to sort the dropdown list based on the Name then call the following

private void populateDataValue()
    {
        using (var db = cnn.ConDB())
        {
            var select = from table in db.SortTables
                         select table;
            DropDownList1.DataSource = select.OrderBy(item => item.Name);
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "SortID";
            DropDownList1.DataBind();
        }
    }


The output looks as:

3.gif

By a simple change in code you can sort based on DataTextField or DataValueField and you can also sort in the descending order by using
select. OrderByDescending (item => item.Name);

Thanks !

Hope this helps.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor