Alex Rivera | Logout

Sorting a DropDownList? - C#, ASP.NET

Asked 2008-10-21T16:39:51.407
36

I'm curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I've looked at a few recommendations but they aren't clicking well with me.

Edit: Folks, I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.

Edit
Report

2 Answers

2

Take a look at the this article from CodeProject, which rearranges the content of a dropdownlist. If you are databinding, you will need to run the sorter after the data is bound to the list.

answered 2008-10-21T22:24:25.160
1
var list = ddl.Items.Cast<ListItem>().OrderBy(x => x.Text).ToList();

ddl.DataSource = list;
ddl.DataTextField = "Text";
ddl.DataValueField = "Value"; 
ddl.DataBind();
answered 2013-02-28T09:51:53.993

Your Answer