GridView排序和分页

我想在我的页面上显示简单的gridview,并为它提供排序和分页function。 单独排序和分页工作正常,但两者的组合没有。 例如,如果我将第一列降序排序然后转到第二页,那么我会看到第二页数据的默认排序(升序)。

我非常依赖这个问题的代码: GridView排序:SortDirection总是升序 ,但问题仍然存在。 另外 – 因为看起来我必须更改我的代码,以便使用Session对象而不是使用ViewState,所以还必须解决这个问题……

我的代码简化了,只有两列:

ASPX:

          

和代码隐藏:

 public partial class TestPage :Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DisplayData(); } } private void DisplayData() { Session["TableView"] = GetUsers(); dgvView.DataSource = Session["TableView"]; dgvView.DataBind(); } protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e) { dgvView.PageIndex = e.NewPageIndex; DisplayData(); } private List GetUsers() { var users = new List(); for (int i = 0; i < 100; i++) { users.Add(new MyUser("Name" + i.ToString().PadLeft(2, '0'), new DateTime(2000, 1, 1).AddDays(i))); } return users; } private class MyUser { public string Name { get; private set; } public DateTime BirthDate { get; private set; } public MyUser(string name, DateTime birthDate) { Name = name; BirthDate = birthDate; } } protected void OnSort(object sender, GridViewSortEventArgs e) { Func f; if (e.SortExpression == "Name") f = u => u.Name; else f = u => u.BirthDate; dgvView.DataSource = Sort((List)Session["TableView"], f, GetSortDirection(e.SortExpression)); dgvView.DataBind(); } private List Sort(IEnumerable user, Func f, SortDirection sortDirection) { if (sortDirection == SortDirection.Ascending) return user.OrderBy(f).ToList(); return user.OrderByDescending(f).ToList(); } private SortDirection GetSortDirection(string column) { string sessionVariable = "TableSort" + column; SortDirection sortDirection; if (Session[sessionVariable] == null) { sortDirection = SortDirection.Ascending; } else if ((SortDirection)Session[sessionVariable] == SortDirection.Ascending) { sortDirection = SortDirection.Descending; } else { sortDirection = SortDirection.Ascending; } Session[sessionVariable] = sortDirection; return sortDirection; } } 

您必须使用ViewState才能保留数据

 public string SortVariable { get { if(ViewState["YourKey"] == null) return string.Empty; return (string)ViewState["YourKey"]; } set { ViewState["YourKey"] = value; } } 

在更改页面时使用此解决方案,您可以在排序之前在视图状态中获得索引排序。

当您触发SortCommand时,您必须设置描述的viewstate的值

你不需要Session,因为你在同一页面,你不会导航

  protected void OnSort(object sender, GridViewSortEventArgs e) { //Here you set your value SortVariable SortVariable = e.SortExpression; ... } 

并在显示数据

您创建数据视图和排序处理

 private void DisplayData() { //GetSortVariable //before bind with dataview you sort } 

问题出在了

 protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e) { dgvView.PageIndex = e.NewPageIndex; DisplayData(); } 

在调用DisplayData()方法时,将网格绑定到未排序的全新数据。

Viewstate保存排序参数(会话对此无用)

如果您使用的是Asp.Net 4为什么不使用数据表或jQGrid ?

我在其他两个答案的帮助下解决了这个问题。

我的解决方案

 public partial class TestPage1 : Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["Data"] = MyUser.GetUsers(); ViewState["SortExpression"] = "Name"; DisplayData("Name", SortDirection.Ascending); } } private void DisplayData(string sortExpression, SortDirection sortDirection) { Func f; if (sortExpression == "Name") f = u => u.Name; else f = u => u.BirthDate; if (sortDirection == SortDirection.Ascending) { dgvView.DataSource = ((IEnumerable)ViewState["Data"]).OrderBy(f).ToList(); } else { dgvView.DataSource = ((IEnumerable)ViewState["Data"]).OrderByDescending(f).ToList(); } dgvView.DataBind(); } protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e) { dgvView.PageIndex = e.NewPageIndex; string sortExpression = ViewState["SortExpression"].ToString(); DisplayData(sortExpression, GetDefaultSortDirection(sortExpression)); } protected void OnSort(object sender, GridViewSortEventArgs e) { ViewState["SortExpression"] = e.SortExpression; ViewState[e.SortExpression] = GetReverseSortDirection(e.SortExpression); DisplayData(e.SortExpression, (SortDirection)ViewState[e.SortExpression]); } private SortDirection GetDefaultSortDirection(string sortExpression) { if (ViewState[sortExpression] == null) { ViewState[sortExpression] = SortDirection.Ascending; } return (SortDirection)ViewState[sortExpression]; } private SortDirection GetReverseSortDirection(string sortExpression) { if (ViewState[sortExpression] == null) { ViewState[sortExpression] = SortDirection.Descending; } else { ViewState[sortExpression] = (SortDirection) ViewState[sortExpression] == SortDirection.Descending ? SortDirection.Ascending : SortDirection.Descending; } return (SortDirection)ViewState[sortExpression]; } }