Sorting a ListBox Control

N

Nathan Sokalski

I have a ListBox Control for which I would like to sort the ListItems. What
is the best way to do this? If possible, can someone show me a simple
example? Any help would be appreciated. Thanks.
 
B

Brock Allen

If your data is in an array, then this approach will work:

string[] data = new string[] {"zzz", "yyy", "bbb", "ccc", "aaa", "xxx"};
Array.Sort(data);

_dropDownList.DataSource = data;
_dropDownList.DataBind();

If your data is in a DataTable/DataSet:

DataSet ds = GetDataSetFromDatabase();
DataView dv = new DataView(ds.Tables["TheTable"]);
dv.Sort = "MyColumn";

_dropDownList.DataSouce = dv;
_dropDownList.DataBind();
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top