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);
        }
Murthy
  • 1,502
  • 11
  • 31
  • 45
  • 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. – Rajesh Jan 09 '12 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 – Murthy Jan 10 '12 at 10:00

1 Answers1

2

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.

Brissles
  • 3,833
  • 23
  • 31