How can sort the item in dropdownlist box?

  • Thread starter Thread starter JL
  • Start date Start date
J

JL

I am used a hashtable for dropdownlist data source but it seems un-sorted on
output.

Anybody who know the sort method in dropdownlist box? thanks a lot
 
Hi,

one way could be adding items (using custom class if you need key & value)
into ArrayList and then sort the ArrayList (probably needs custom comparer
class) and then bind this ArrayList to the DDL.
 
ArrayList al=new ArrayList(thedict);
al.Sort(new YourComparer());
ddl.DataSource=al;
ddl.DataTextField="Value";
ddl.DataValueField="Key";


public class YourComparer : IComparer
{
public int Comparer(object x,object y)
{
DictionaryEntry a=(DictionaryEntry)x;
DictionaryEntry b=(DictionaryEntry)y;
return a.Key.ToString().CompareTo(b.Key.ToString());
}
}
 
thanks a lot
Lostinet.Web Support said:
ArrayList al=new ArrayList(thedict);
al.Sort(new YourComparer());
ddl.DataSource=al;
ddl.DataTextField="Value";
ddl.DataValueField="Key";


public class YourComparer : IComparer
{
public int Comparer(object x,object y)
{
DictionaryEntry a=(DictionaryEntry)x;
DictionaryEntry b=(DictionaryEntry)y;
return a.Key.ToString().CompareTo(b.Key.ToString());
}
}

--
http://www.lostinet.com/
Lostinet.Web Controls&Components
ComboBox,ComboCalendar DatePicker,SafePwdBox,SmartIFrame,
AlertElement,ConfirmElement,PromptElement,DialogElement,ControlHelper
 
thanks a lot
Teemu Keiski said:
Hi,

one way could be adding items (using custom class if you need key & value)
into ArrayList and then sort the ArrayList (probably needs custom comparer
class) and then bind this ArrayList to the DDL.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


un-sorted
 
Back
Top