允许GridView排序ASP C#

时间:2016-08-19 21:17:49

标签: c# sorting gridview datatable datasource

我有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:数据源不支持排序。

如何启用对DataTable的DataSource进行排序?

2 个答案:

答案 0 :(得分:0)

在后面的代码中对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();

答案 1 :(得分:0)

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

排序功能(不要忘记在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;
    }