Sorting in Drop down list based on database columns.

Posted by Madhuk under LINQ category on | Points: 40 | Views : 2755
<asp:DropDownList ID="DropDownList1" runat="server">

// Linq to Sql Object
LinqDataContext sortLinqDataContext = new LinqDataContext();
//SortTables is the data base table with columns Name, SortID.

var select = from table in LinqDataContext.SortTables
select table;
//sorted based on SortID.
DropDownList1.DataSource = select.OrderBy(item => item.SortID);
//sorted based on Name.
DropDownList1.DataSource = select.OrderBy(item => item.Name);
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "SortID";

Comments or Responses

Login to post response