Thursday, September 9, 2010

C#: Sort DropDownList

public void SortDropDownList(ref DropDownList ddl)
{
DropDownList ddlClone = new DropDownList();
foreach (ListItem item in ddl.Items)
{
ddlClone.Items.Add(item);
}
var query = ddlClone.Items.Cast().OrderBy(o => o.Text);
ddl.Items.Clear();
foreach (var item in query)
{
ddl.Items.Add(item);
}
}