繁体   English   中英

当我在asp.net webform C#中移动到GridView的下一页时,排序会丢失

[英]Sorting is lost when i move to next page of GridView in asp.net webform C#

我有一个简单的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();
        }
    }

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

我建议设置一个断点:

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

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

愿这篇文章帮助你排序 - 不在网上工作 - 在aspview网上工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM