Reputation: 766
Is it possible to sort an ASP.NET GridView using DataTable.Select("", sortExpression)?
I have a regular GridView with AllowSorting="true" and OnSorting="grdEmployees_Sorting".
protected void grdEmployees_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = getDataTable();
var sortExprOrder = e.SortDirection == SortDirection.Ascending ? " ASC" : " DESC";
var rows = dt.Select("", string.Format(e.SortExpression + "{0}", sortExprOrder));
grdEmployees.DataSource = rows;
grdEmployees.DataBind();
}
Not sure why, but this does not work. The grid shows a bunch of rows with three columns, RowError, RowState, and HasErrors (contains rows with all empty checkboxes).
Am I doing something wrong?
Upvotes: 0
Views: 655
Reputation: 1985
You don't need select for sorting DataTable.Select is for filtering
This how you sort
protected void grdEmployees_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = getDataTable();
var sortExprOrder = e.SortDirection == SortDirection.Ascending ? " ASC" : " DESC";
DataView dv = new DataView(dt);
dv.Sort = string.Format("{0} {1}",
e.SortExpression, sortExprOrder);
grdEmployees.DataSource = dv;
grdEmployees.DataBind();
}
Upvotes: 1