views:

184

answers:

2

Hi Guys,

I have a dropdown in which we add certain items after the dropdown is bound by data from the db, hence the need to sort the dropdown arises. So i need to sort a dropdown which can have duplicates. What is the best way of doing this?

+1  A: 

Assuming you are binding to a Generic List you can try something like this :

    var ddlFoo = new List<foo>();
    foreach (var lc in myDropDownList.Items)
    {
        ddlFoo.Add((foo)lc);
    }
    myDropDownList.DataSource = ddlFoo.OrderBy(dl => dl.fooID);
    myDropDownList.Databind();
Drahcir
+2  A: 

Instead of adding items directly to the Dropdown, I would suggest adding them to the data structure that you bind to. If the items in this structure implement IComparable, then you can define a comparison method to apply sorting before the Dropdown is actually bound to the data source.

Cerebrus