Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top