Advertisements
RSS

Tag Archives: Datatabe

C#:Using Dictionary as DropDownList Data Source

I have IDictionary Object called ListOptions. To use it as the data source for an ASP.NET DropDownList called objDropDownList:

 

objDropDownList.DataSource = ListOptions;

objDropDownList.DataTextField = “Value”;

          objDropDownList.DataValueField  = “Key”;

          objDropDownList.DataBind(); 

 

 

 

Advertisements
 
Leave a comment

Posted by on March 31, 2009 in Programming

 

Tags: , , , , , , , , , , , , , ,

How to sort a collection to be used in DropDown List

I have a collection object (EmployeesCollection) for custom Employee class, In the Employee class there are two properties Name and EmployeeID. I want to sort the EmployeesCollection by Name and use it in a DropDown List (ddEmployees).

 

DataTable dtEmployees = new DataTable();

          dtEmployees.Columns.Add(“Name”);

          dtEmployees.Columns.Add(“EmployeeID”);

          foreach (Employee vEmployee in EmployeesCollection)

          {

                    dtEmployees.Rows.Add(new object[] { vEmployee.Name, vEmployee. EmployeeID  });

          }

          dtEmployees.DefaultView.Sort = “Name”;

          ddEmployees.DataSource = dtEmployees;

          ddEmployees.DataTextField = “Name”;

          ddEmployees.DataValueField = “EmployeeID”;

          ddEmployees.DataBind();

 

 

 

 

 
Leave a comment

Posted by on February 18, 2009 in Programming

 

Tags: , , , , , , , , , , , ,

 
%d bloggers like this: