允许GridView排序

本文关键字:排序 GridView 允许 | 更新日期: 2023-09-27 18:03:21

我有一个GridView,我使用sqldatasource填充,我使用标题排序没有任何问题,通过启用AllowSorting="true"

然后我使用不同的控制器过滤这个GridView,我使用DataTable运行过滤功能如下:

    var mySqlConnection = //mySqlConnection
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
        cmd = new SqlCommand("SPRlist_GetSPRCombine", mySqlConnection);
        cmd.Parameters.AddWithValue("@SPRKaimrcNo", sprkaimrcno);
        cmd.Parameters.AddWithValue("@SPRNo", sprno);
        cmd.Parameters.AddWithValue("@DateOfRequest", dateofrequest);
        cmd.Parameters.AddWithValue("@RequesterBadge", requesterbadge);
        cmd.Parameters.AddWithValue("@DeptID", department);
        cmd.Parameters.AddWithValue("@SPRStatus", sprstatus);
        mySqlConnection.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        da.SelectCommand = cmd;
        da.Fill(dt);

我调用传递控制器变量的函数,它给我返回确切的结果:

    GridViewSPRlist.DataSourceID = "";
    //Data Table Function Passing Controllers
    GridViewSPRlist.DataBind();

问题是:如果GridView被过滤,我无法对它进行排序,我得到以下错误:

数据源不支持排序。描述:在执行当前web请求期间发生了未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。异常详细信息:System。NotSupportedException:数据源不支持排序。

如何启用排序数据源的数据表?

允许GridView排序

在后面的代码中排序DataTable本身并将其绑定到GridView。也许这就是解决你问题的办法?

dt.DefaultView.Sort = "sortExpression DESC";
//use DefaultView.ToTable() if you want to use the sorted datatable later on in a viewstate or session etc.
//dt.DefaultView.ToTable();
GridViewSPRlist.DataSource = dt;
GridViewSPRlist.DataBind();

我在合并所有我找到的解决方案后找到了解决方案。我在加载时删除了sqldatasource,并将其替换为相同的DataTable,将空参数传递给函数以检索整个数据集

用于排序的函数(不要忘记在GridView OnSorting上调用它):

protected void GridViewSPRlist_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dt = Session["TaskTable"] as DataTable;
    if (dt != null)
    {
        //Sort the data.
        dt.DefaultView.Sort = e.SortExpression + " " + (GetSortDirection(e.SortExpression));
        GridViewSPRlist.DataSource = Session["TaskTable"];
        GridViewSPRlist.DataBind();
    }
}
private string GetSortDirection(string column)
{
    // By default, set the sort direction to ascending.
    string sortDirection = "ASC";
    // Retrieve the last column that was sorted.
    string sortExpression = ViewState["SortExpression"] as string;
    if (sortExpression != null)
    {
        // Check if the same column is being sorted.
        // Otherwise, the default value can be returned.
        if (sortExpression == column)
        {
            string lastDirection = ViewState["SortDirection"] as string;
            if ((lastDirection != null) && (lastDirection == "ASC"))
            {
                sortDirection = "DESC";
            }
        }
    }
    // Save new values in ViewState.
    ViewState["SortDirection"] = sortDirection;
    ViewState["SortExpression"] = column;
    return sortDirection;
}

DataTable功能:

public DataTable GetSPRCombine(//Your Param)
{
    var mySqlConnection = //Your Connection String
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
        cmd = new SqlCommand("SPRlist_GetSPRCombine", mySqlConnection);
        //Your command parameters
        mySqlConnection.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        da.SelectCommand = cmd;
        da.Fill(dt);
        Session["TaskTable"] = dt;
    }