user3091887
user3091887

Reputation: 105

how can i sort drop down items in asp.net?

I have a drop down in asp.net that I added some things to from the database. Also in the end I added some things manually. Now I need to sort these items in a quick and simple way. The drop down selected value is as number.

Is object link useful for my problem? If your answer is yes, please describe.

Upvotes: 4

Views: 24688

Answers (5)

Md Shabbeer Ali
Md Shabbeer Ali

Reputation: 11

To sort dropdownlist items in simple way. First fill the dataset through adapter then fill dataview from dataset and sort the dataview with required column.Finally bind the dropdownlist with dataview.

Step 1: Create connection object and then fill dataset using dataadapter

Example:

I) creating connection object as follows:

SqlConnection con=new SqlConnection("Data Source=servername;Database=dbname;Integrated Security=true"); //if windows authentication 
(or) 
SqlConnection con=new SqlConnection("Data Source=servername;Database=dbname;user id=xxx;pwd=xxx"); //if sql authentication 

II) creating object for adapter class with query and connection object as two parameters as follows:

SqlDataAdapter sda=new SqlDataAdapter("query",con);

Step 2: Create DataSet object and fill it using adapter object as follows:

DataSet ds=new DataSet();
sda.fill(ds);// filling sda data into dataset using fill method of adapter class

Step 3: Check dataset is non-empty or not. If non-empty then create DataView object and fill it with sorted option and bind dropdownlist as follows:

if(ds.Tables[0].rows.count>0)
{
    DataView dv=new DataView();
    dv.Table=ds.Tables[0]; // filling dataview with dataset
    dv.sort="columnname";
    DropDownList1.DataSource=dv;
    DropDownList1.DataTextField="columnname";
    DropDownList1.DataBind();
    DropDownList1.Items.Insert(0,"select");
}

Upvotes: 1

Win
Win

Reputation: 62260

You can create a small utility method like this to sort the items of DropDownList.

public static void SortListControl(ListControl control, bool isAscending)
{
    List<ListItem> collection;

    if (isAscending)
        collection = control.Items.Cast<ListItem>()
            .Select(x => x)
            .OrderBy(x => x.Text)
            .ToList();
    else
        collection = control.Items.Cast<ListItem>()
            .Select(x => x)
            .OrderByDescending(x => x.Text)
            .ToList();

    control.Items.Clear();

    foreach (ListItem item in collection)
        control.Items.Add(item);
}

Usage

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
        DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));

    // Sort the DropDownList's Items by descending
    SortListControl(MyDropDownList, false);
}

Upvotes: 6

Chandan Kumar
Chandan Kumar

Reputation: 4638

Use this :

        SortedList<int, string> mySortedList = new SortedList<int, string>();
        mySortedList.Add(1, "Hi");
        mySortedList.Add(2, "Hello");
        mySortedList.Add(3, "German");

        dropDownList1.DataTextField = "Value";
        dropDownList1.DataValueField = "Key";
        dropDownList1.DataSource = mySortedList;
        dropDownList1.DataBind();

Upvotes: 1

Harrison
Harrison

Reputation: 3953

Without code it is going to be hard to answer your question. But this is most likely what you are looking for. IEnumerable.OrderBy()

Sorts the elements of a sequence in ascending order according to a key.

Upvotes: 1

Tim
Tim

Reputation: 4101

You could set it up as a SortedList and then just call the list.Sort() method.

You would set the list up as your datasource, and use the key/value fields as the DataTextField and DataValueField.

StackOverflow question on sorted list as datasource

Upvotes: 0

Related Questions