Allen Buckley's Blog
My experiences in .Net

DropDownList Sort using LINQ

Wednesday, September 02, 2009 3:47 PM

I needed to do a quick sort on a DropDownList Server Control and found a great way of doing it in just 4 lines of code.  Your project will need to be pointed to the .NET 3.5 Framework and will need a reference to System.Linq.

 

List<string> list = DropDownList.Items.Cast<ListItem>().Select(item => item.Text).ToList(); 
list.Sort((x, y) => string.Compare(x, y)); 
DropDownList.DataSource = list; 
DropDownList.DataBind();



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: DropDownList Sort using LINQ

That method throws away all the drop down values, doesn't it!!? 2/28/2010 9:03 PM | Perry

# re: DropDownList Sort using LINQ

This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. 12/11/2011 9:20 PM | wholesale dropship products

Post a comment