Sort and bind datarow to dropdownlist
string _Code =开发者_C百科 "21";
IEnumerable<DataRow> drs = GetCodes(_Code);
foreach (DataRow items in drs)
{
ListItem li = new ListItem(items["CallingCode"].ToString(), items["CountryID"].ToString());
ddCountry.Items.Add(li);
}
//How can i sort the dropdownlist by calling code.
This might help...
Sort IEnumerable and List with property name
You could use LINQ to sort drs
before creating the list of items...
var orderedDRS = drs.OrderBy(row => row["CallingCode"].ToString());
foreach (DataRow items in orderedDRS)
{
...
}
Where row => row["CallingCode"].ToString()
is a lambda expression that selects the row's "CallingCode" as the sort key for OrderBy
You could use LINQ further to bind the DropDown without a foreach
loop by Selecting
out the CallingCode and CountryID values
精彩评论