Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET
Article

Sorting Dropdown list in ASP.NET using C#

Rate me:
Please Sign up or sign in to vote.
2.45/5 (15 votes)
21 Aug 2007 143.7K   25   11
There is a simple solution for sorting Dropdown list in ASP.NET

Introduction

I found a big problem in ASP.NET is there was no direct method for sorting the items in the Dropdown list. Sometimes this sorting functionality is required in the applications.

Here I gave a small and easy technique of sorting the Dropdown list in ASP.NET

Using the code

I assumed that, you have a Dropdown list in your ASPX page with name "DropDownList1". I have added few items to this Dropdown list in the Page_Load event with disorder manner.

protected void Page_Load(object sender, EventArgs e)
{
    this.DropDownList1.Items.Add("Orange");
    this.DropDownList1.Items.Add("Grapes");
    this.DropDownList1.Items.Add("Apple");
    this.DropDownList1.Items.Add("Mango");
    this.DropDownList1.Items.Add("Lemon");
    this.DropDownList1.Items.Add("Banana");

SortDDL(ref this.DropDownList1);
}

In the Page_Load event, I have called a function "SortDDL" for sorting the Dropdown list. In this function we have to pass the reference of the required Dropdown list. Then system will automatically sort the items in the given Dropdown list.

 private void SortDDL(ref DropDownList objDDL)
 {
    ArrayList textList = new ArrayList();
    ArrayList valueList = new ArrayList();


    foreach (ListItem li in objDDL.Items)
    {
        textList.Add(li.Text);
    }

    textList.Sort();


    foreach (object item in textList)
    {
        string value = objDDL.Items.FindByText(item.ToString()).Value;
        valueList.Add(value);
    }
    objDDL.Items.Clear();

for(int i = 0; i < textList.Count; i++)
    {
        ListItem objItem = new ListItem(textList[i].ToString(), valueList[i].ToString());
        objDDL.Items.Add(objItem);
    }
 }

We should need to import the following namespace for getting ArrayList class.

<p style="TEXT-INDENT: 0.5in">using System.Collections; 

</p>

Happy Coding to All!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
India India
I am Rajesh Babu, currently working for Nous Infosystems, Bangalore. I have 6 plus years experience in Microsoft Technologies.

Comments and Discussions

 
QuestionConverting to VB Pin
Timothy Vandeweerd13-Nov-14 9:11
Timothy Vandeweerd13-Nov-14 9:11 
GeneralSortableDropDownList Pin
sross9010-May-09 23:37
sross9010-May-09 23:37 
GeneralGood article! Pin
indpride128-Aug-07 20:01
indpride128-Aug-07 20:01 
GeneralRe: Good article! Pin
Member 448198722-May-08 0:34
Member 448198722-May-08 0:34 
GeneralVery Good Article Pin
prr77715-Apr-10 0:05
prr77715-Apr-10 0:05 
GeneralHi, 3 loops ..... Pin
kaken2022-Aug-07 0:59
kaken2022-Aug-07 0:59 
GeneralRe: Hi, 3 loops ..... Pin
Rafael Nicoletti22-Aug-07 2:16
Rafael Nicoletti22-Aug-07 2:16 
GeneralRe: Hi, 3 loops ..... Pin
Jay Thakar5-Feb-10 5:32
Jay Thakar5-Feb-10 5:32 
one has to learn; that is life of developer Smile | :)
GeneralC# example Pin
Adam Marzi4-Feb-09 12:32
Adam Marzi4-Feb-09 12:32 
GeneralRe: C# example Pin
senthilpsp11-Feb-09 0:16
senthilpsp11-Feb-09 0:16 
GeneralRe: C# example Pin
UdeS23-Mar-09 2:45
UdeS23-Mar-09 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.