net gridview中的分页和排序问题

时间:2022-11-30 16:27:22

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

我有一个网格视图,其中实现了分页和排序。当我对表进行排序并选择第2页时,排序就丢失了,而在gridview绑定中所提到的desc中显示了第二个20条记录。

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 个解决方案

#1


2  

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

您可以使用ViewState字符串vars对源查询进行排序,以存储排序字段和排序方向:

Ensure ViewState vars are set with defaults:

确保ViewState vars设置为默认值:

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:

包括在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.

这样,您的源数据是预先排序的,您不需要担心使用myView。排序或运行到任何分页/排序冲突。

#2


0  

Please refer to this article about paging and sorting in asp.net gridview.

请参考这篇关于asp.net gridview中分页和排序的文章。

Hope that helps.

希望有帮助。

#1


2  

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

您可以使用ViewState字符串vars对源查询进行排序,以存储排序字段和排序方向:

Ensure ViewState vars are set with defaults:

确保ViewState vars设置为默认值:

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:

包括在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.

这样,您的源数据是预先排序的,您不需要担心使用myView。排序或运行到任何分页/排序冲突。

#2


0  

Please refer to this article about paging and sorting in asp.net gridview.

请参考这篇关于asp.net gridview中分页和排序的文章。

Hope that helps.

希望有帮助。