【问题标题】:Allow GridView Sorting ASP C#允许 GridView 排序 ASP C#
【发布时间】:2016-12-27 03:10:24
【问题描述】:

我有一个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 的排序?

【问题讨论】:

  • @TonyDong 我已经检查过了,但对我不起作用!
  • 你在 html gridview 上添加了 OnSorting="gridView_Sorting" 吗?
  • @TonyDong 是的,我做到了,实际上,在我使用您提到的链接中的函数后,即使在 sqldatasource 中工作的排序也不起作用

标签: c# sorting gridview datatable datasource


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在合并我找到的所有解决方案后,我找到了解决方案。 我在加载时删除了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;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-06-02
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多