Digital Colony!

SortedList Example for ASP.NET in C#

The SortedList object uses a key/value combination to sort a list. The SortedList can be databound to the following ASP.NET controls:
  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:ListBox
Our examples will bind a SortedList to an asp:ListBox.
<asp:ListBox ID="lsbPresidents" runat="server" />

Numeric Sort

SortedList s = new SortedList();
s.Add(16, "Lincoln");
s.Add(42, "Clinton");
s.Add(40, "Reagan");
s.Add(1, "Washington");

lsbPresidents.DataSource = s;
lsbPresidents.DataValueField = "key";
lsbPresidents.DataTextField = "value";
lsbPresidents.DataBind();
Results:
Washington
Lincoln
Reagan
Clinton      

Alphabetic

SortedList s = new SortedList();
s.Add("L", "Lincoln");
s.Add("C", "Clinton");
s.Add("R", "Reagan");
s.Add("W", "Washington");

lsbPresidents.DataSource = s;
lsbPresidents.DataValueField = "key";
lsbPresidents.DataTextField = "value";
lsbPresidents.DataBind();
Results:
Clinton   
Lincoln  
Reagan 
Washington

Date Sort

SortedList s = new SortedList();
s.Add(Convert.ToDateTime("2/12/1809"), "Lincoln");
s.Add(Convert.ToDateTime("8/19/1946"), "Clinton");
s.Add(Convert.ToDateTime("2/6/1911"), "Reagan");
s.Add(Convert.ToDateTime("2/22/1732"), "Washington");

lsbPresidents.DataSource = s;
lsbPresidents.DataValueField = "key";
lsbPresidents.DataTextField = "value";
lsbPresidents.DataBind();
Results:
Washington
Lincoln
Reagan
Clinton      

Labels:

posted by MAS on Jan 2,2007

If you found this site helpful, consider buying me a cup of coffee. Smile

Digital Colony Copyright © 1999-2010 XHTML   508
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.