Esha
Esha

Reputation: 389

Sort an unbound dropdownlist

I have a dropdownlist which I have populated with the following code:

statusList.Items.Add(new ListItem(Inactive_AddUser, "0"));
statusList.Items.Insert(0, new ListItem(Select, Select));
if (activeUser < (Convert.ToInt32(Session["MaxActiveUsers"])))
 {
   statusList.Items.Add(new ListItem(Active_AddUser, "1"));
 }

Now I'm unable to sort the list. Any suggestions on how can I implement it?

Upvotes: 0

Views: 407

Answers (1)

Majid
Majid

Reputation: 14253

You can first create a List of ListItems then sort it and after all bind it to DropDownList some thing like this that I found from here:

List<ListItem> items;//fill this with your own items
items.Sort((x, y) => x.Text.CompareTo(y.Text));
statusList.DataSource = items;
statusList.DataBind();

Upvotes: 1

Related Questions