I'm using this code to sort values in dropdownlist :
DropDownList1.DataSource = DropDownList1.Items.Cast<ListItem>()
.OrderBy(o => o.Value).ToList();
ddlApoeni.DataBind();
the problem is that result is descending order, but i want ascending ?
Loading
Master BillaPosted Sep 14, 2009, 11:50 AM
When you are call OrderBy its give the ascending order.
Here my complete sample code
public static IEnumerable
{
sortExpression += "";
string[] parts = sortExpression.Split(' ');
bool descending = false;
string property = "";
if (parts.Length > 0 && parts[0] != "")
{
property = parts[0];
if (parts.Length > 1)
{
descending = parts[1].ToLower().Contains("esc");
}
PropertyInfo prop = typeof(T).GetProperty(property);
if (prop == null)
{
throw new Exception("No property '" + property + "' in + " + typeof(T).Name + "'");
}
if (descending)
return list.OrderByDescending(x => prop.GetValue(x, null));
else
return list.OrderBy(x => prop.GetValue(x, null));
}
return list;
}
Thank you