This blog is moved to
http://amalhashim.wordpress.com

Friday, May 15, 2009

Sorting DropDownList Using Linq

C#
protected void btnSortNames_Click(object sender, EventArgs e)
{
cboNames.DataSource = cboNames.Items.Cast<ListItem>()
.OrderByDescending(o => o.Text)
.ToList();
cboNames.DataBind();
}
protected void btnSortAscending_Click(object sender, EventArgs e)
{
cboNames.DataSource = cboNames.Items.Cast<ListItem>()
.OrderBy(o => o.Text)
.ToList();
cboNames.DataBind();
}


VB.NET


Protected Sub btnSortNames_Click(ByVal sender As Object, ByVal e As EventArgs)
cboNames.DataSource = cboNames.Items.Cast(Of ListItem)().OrderByDescending(Function(o) o.Text).ToList()
cboNames.DataBind()
End Sub
Protected Sub btnSortAscending_Click(ByVal sender As Object, ByVal e As EventArgs)
cboNames.DataSource = cboNames.Items.Cast(Of ListItem)().OrderBy(Function(o) o.Text).ToList()
cboNames.DataBind()
End Sub

No comments: