Display items from dropdown list in alphabetical order

S

Siew Yee

Hi,

Is there any way where I can display items from dropdown list in
alphabetical order?
Thanks.
 
Y

Yahoo

ASP.NET framework does not have a sort method on the dropdown list as you
probably noticed. You have two options and they are about equally.

1) sort the list before you do your databind().

2) create a class that inherits from DropDownList and supply your own
method. You would probably want to do something like...

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;

namespace JoesExample
{
public class MyDropDownList : System.Web.UI.WebControls.DropDownList
{
public void Sort()
{
ListItemCollection lic = base.Items;
ArrayList al = new ArrayList(lic);
al.Sort(new MyComparer());

lic.Clear();
lic.AddRange( (ListItem[])al.ToArray(typeof(ListItem)) );
}

private class MyComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare( ((ListItem)x).Text,
((ListItem)y).Text );
}
}
}
}

Joe
MCAD
SRE (Simple Rule Engine)
https://sourceforge.net/projects/sdsre/
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top