Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
object[] SortedItems = new object[DropDownList1.Items.Count];
for (int i = 0; i < SortedItems.Length; i++)
{
SortedItems[i] = DropDownList1.Items[i];
}
Array.Sort(SortedItems);
DropDownList1.Items.Clear();
DropDownList1.Items.AddRange(SortedItems);

In the above code I copied items in DropDownList to ArrayList, I sorted the ArrayList, cleared the DropDownList.

I could not add the sorted items to the DropDownList.

Thanks in advance.
Posted
Updated 2-Mar-12 23:38pm
v2
Comments
André Kraak 3-Mar-12 5:38am    
Edited question:
Added pre tags
Sergey Alexandrovich Kryukov 3-Mar-12 21:17pm    
Don't use obsolete ArrayList, it is non-generic; such type were rendered obsolete by v.2.0.
--SA

Refer this link. It will help you.
Sorting Dropdown list in ASP.NET using C#[^]
 
Share this answer
 
Comments
Ravi shekahr 3-Mar-12 1:23am    
when data come from database then ????
psgviscom 3-Mar-12 1:55am    
Use orderby in your database
refer this link
http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
Ravi shekahr 3-Mar-12 2:21am    
i tried that code but not working .please help me
psgviscom 4-Mar-12 23:41pm    
Try my another solution.(solution3)
Refer following links for diff. solutions
using LINQ:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=312[^]
or solution1 or refer
Sort Method for an ASP.NET DropDownList[^]
or use ddlId.Sorted=True; if u r binding that DDL
 
Share this answer
 
Refer this links.
This shows how to give dropdown's datasource as database.
http://www.dbtutorials.com/advanced/populatingdropdown-csharp.aspx[^]


This link shows how to sort data in dropdownlist.
http://abhijitjana.net/2010/08/08/how-to-sort-asp-net-dropdownlist-based-on-datavaluefield-or-datatextfield-using-linq/[^]


Combine these two and write your code.
Hope this helps you.

All the best.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900