简体   繁体   中英

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

I have a simple gridview to display records, and i am using sorting to sort by all columns. When am on first page, sorting works by when i move to next page then it shows me un sorted records again.

    <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" >

CODE BEHIND

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();
        }
    }

I am not sure what is causing this

I would suggest setting a breakpoint at:

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

and check if the value of SortDirection is as expected. If not there is a issue with storing the order in the ViewState. If not you it is something in the DataView.

May this post help you sorting-is-not-working-on-gridview-in-asp-net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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