Sorting a ListBox Control

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
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.
 
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();


-Brock
DevelopMentor
http://staff.develop.com/ballen
 
Back
Top