ASP.NET GridView使用DataTable.Select排序

时间:2017-04-15 23:04:58

标签: c# asp.net gridview

是否可以使用DataTable.Select("",sortExpression)对ASP.NET GridView进行排序?

我有一个常规的GridView,带有AllowSorting =" true"和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();
        }

不确定原因,但这不起作用。网格显示了一堆包含三列的行,RowError,RowState和HasErrors(包含所有空复选框的行)。

我做错了吗?

1 个答案:

答案 0 :(得分:1)

您不需要选择排序DataTable.Select用于过滤

这是你如何排序

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();
        }