How can sort the item in dropdownlist box?

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
 
T

Teemu Keiski

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.
 
L

Lostinet.Web Support

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());
}
}
 
J

JL

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
 
J

JL

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top