分页和asp.net gridview的排序问题 [英] Issues with paging and sorting in asp.net gridview
问题描述
我在哪里,我都实现分页和排序网格视图。当我对表进行排序,然后选择第2页,则排序会丢失,在GridView控件绑定提到的第二个20记录显示在递减
私有数据集BindGridView(串场)
{
DataSet的DS =新的DataSet()
字符串userQuery =从tbl_user为了通过tbl_user.UserID递减选择tbl_User.UserID,tbl_User.FirstName
UserTable.DataBind();
返回DS;
}< ASP:GridView控件ID =用户表=服务器每页=20AllowPaging =真
的SelectedIndex =0的DataKeyNames =用户名OnRowDataBound =UserTable_RowDataBound
的AutoGenerateColumns =false的OnPageIndexChanging =gridView_PageIndexChangingAllowSorting =真OnSorting =gridView_Sorting>
我怎样才能留住排序和进行分页,我的那种状态存储在会话,我怎么可以用它来执行分页。
保护无效gridView_PageIndexChanging(对象发件人,GridViewPageEventArgs E)
{
UserTable.PageIndex = e.NewPageIndex;
UserTable.DataBind();
数据视图MyView的=新数据视图(BindGridView(会话[USEREMAIL]的ToString())表[0]);
} 保护无效gridView_Sorting(对象发件人,GridViewSortEventArgs E)
{
字符串SORTEX pression = NULL;
如果((会话[UsersortEx pression] == NULL))
{
SORTEX pression = NULL;
}
其他
{
SORTEX pression =(会话[UsersortEx pression]的ToString());
}
如果(SORTEX pression == e.SortEx pression)
{
SORTEX pression + =DESC;
}
其他
{
SORTEX pression = e.SortEx pression;
}
数据视图MyView的=新数据视图(BindGridView(会话[USEREMAIL]的ToString())表[0]);
myView.Sort = SORTEX pression;
UserTable.DataSource = MyView的;
UserTable.DataBind();
//保存排序状态
Session.Add(UsersortEx pression,SORTEX pression);
}
您可以适用于你的源查询使用ViewState的字符串瓦尔进行排序存放排序的字段的和排序的方向的
确保ViewState的瓦尔被设置为默认设置:
的ViewState [sortDir] =DESC;
的ViewState [的SortField] =tbl_user.UserID;
然后,修改您的查询字符串:
字符串userQuery =
选择tbl_User.UserID,tbl_User.FirstName+
从tbl_user+
ORDER BY+(字符串)的ViewState [的SortField] ++(字符串)的ViewState [sortDir];
包含在OnSorting:
保护无效gridView_Sorting(对象发件人,GridViewSortEventArgs E)
{
//设置新的排序方向和排序字段
如果((字符串)的ViewState [sortDir] ==降序){的ViewState [sortDir] =ASC;}
其他{的ViewState [sortDir] =DESC; }
的ViewState [的SortField] = e.SortEx pression; //重新绑定
...
}
这样,你的源数据是pre-排序,你不必担心使用 myView.Sort
或运行任何分页/排序冲突
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);
}
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.
这篇关于分页和asp.net gridview的排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!