【问题标题】:Sorting is lost when i move to next page of GridView in asp.net webform C#当我在 asp.net webform C# 中移动到 GridView 的下一页时,排序丢失
【发布时间】:2017-09-29 21:39:20
【问题描述】:

我有一个简单的 gridview 来显示记录,并且我正在使用排序来按所有列排序。当我在第一页时,当我移动到下一页时排序工作,然后它再次向我显示未排序的记录。

    <asp:GridView ID="gv" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ID" 
Width="1100px" BackColor="White" BorderColor="#f5f5f5" BorderStyle="None" BorderWidth="0px" CellPadding="5"                 Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" GridLines="Horizontal" PageSize="500" CssClass="myheader" OnPageIndexChanging="gv_PageIndexChanging" OnSorting="gv_Sorting"  AllowSorting="true"  onrowdatabound="gv_RowDataBound" onrowcommand="gv_RowCommand" >

代码隐藏

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
       // getCareerList();

       // DataTable dataTable = gv.DataSource as DataTable;

        DataSet ds = new DataSet();
        ds = DataProvider.GetFormByFormTypeIDForGridview(int.Parse(ddCareerType.SelectedItem.Value.ToString()));
        DataTable dataTable = ds.Tables[0];

        string SortDirection = "DESC";
        if (ViewState["SortExpression"] != null)
        {
            if (ViewState["SortExpression"].ToString() == e.SortExpression)
            {
                ViewState["SortExpression"] = null;
                SortDirection = "ASC";
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
            }
        }
        else
        {
            ViewState["SortExpression"] = e.SortExpression;
        }


        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + SortDirection;
            gv.DataSource = dataView;
            gv.DataBind();
        }
    }

我不确定是什么原因造成的

【问题讨论】:

    标签: c# asp.net sorting gridview


    【解决方案1】:

    我建议在以下位置设置断点:

    dataView.Sort = e.SortExpression + " " + SortDirection;
    

    并检查 SortDirection 的值是否符合预期。如果不是,则将订单存储在 ViewState 中会出现问题。如果不是你,那就是 DataView 中的东西。

    愿这篇文章对您有所帮助sorting-is-not-working-on-gridview-in-asp-net

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 2018-02-23
      • 2023-03-12
      • 2011-06-23
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多