3

I have a grid view where I am implementing both paging and sorting. When I sort the table and select page 2, then the sorting is lost and the second 20 records are displayed in desc as mentioned in gridview binding

private DataSet BindGridView(string field)
        {
         DataSet ds = new DataSet()
      string userQuery = "Select tbl_User.UserID, tbl_User.FirstName from tbl_user order by tbl_user.UserID desc";
      UserTable.DataBind();
            return ds;
        }



<asp:GridView ID="UserTable" runat="server"   PageSize="20" AllowPaging="True"
                    SelectedIndex="0" DataKeyNames="UserID"  OnRowDataBound="UserTable_RowDataBound"                   
   AutoGenerateColumns="false" OnPageIndexChanging="gridView_PageIndexChanging"  AllowSorting="true" OnSorting="gridView_Sorting">

How can I retain the sorting and perform the paging, I store the sort state in the session, how can i use that to perform paging.

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            UserTable.PageIndex = e.NewPageIndex;        
            UserTable.DataBind();
            DataView myView = new DataView(BindGridView(Session["useremail"].ToString()).Tables[0]);
        }

     protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = null;
            if ((Session["UsersortExpression"] == null))
            {
                sortExpression = null;
            }
            else
            {
                sortExpression = (Session["UsersortExpression"].ToString());
            }
            if (sortExpression == e.SortExpression)
            {
                sortExpression += " DESC";
            }
            else
            {
                sortExpression = e.SortExpression;
            }
            DataView myView = new DataView(BindGridView(Session["useremail"].ToString()).Tables[0]);
            myView.Sort = sortExpression;
            UserTable.DataSource = myView;
            UserTable.DataBind();
            //save sort state 
            Session.Add("UsersortExpression", sortExpression);
        }
2
  • Seems like you are rebinding the grid in your page index changed and again in your BindGridView. Do no rebind the grid multiple times. Just make sure that your BindGridView method does the binding after checking your pageindex and the sorting logic. Commented Jan 9, 2012 at 17:26
  • If I dont call the bindgridview from paging codebehind, then page 2 is not filled, it has no records. How can I do it, can you show some code Commented Jan 10, 2012 at 10:00

1 Answer 1

3

You could apply sorting on your source query using ViewState String vars to store sort field and sort direction:

Ensure ViewState vars are set with defaults:

ViewState["sortDir"] = "DESC"; 
ViewState["sortField"] = "tbl_user.UserID";

Then, modify your query string:

string userQuery = 
"Select tbl_User.UserID, tbl_User.FirstName " +
"from tbl_user " + 
"ORDER BY " + (String)ViewState["sortField"] + " " + (String)ViewState["sortDir"];

Include in OnSorting:

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
    // Set new sort direction and sort field
    if ((String)ViewState["sortDir"] == "DESC"){ViewState["sortDir"] = "ASC";}
    else { ViewState["sortDir"] = "DESC"; }
    ViewState["sortField"] = e.SortExpression;

    // Rebind
    ...     
}

This way, your source data is pre-sorted and you don't need to worry about using myView.Sort or running into any paging/sorting conflicts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.