排序Gridview问题



我有一个gridview,我正在使用分页

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   GridView1.PageIndex = e.NewPageIndex;
   this.BindData();
}

现在的问题是,当我点击标题排序网格视图,然后我去到第二页,然后回到第一个,它不记得排序表达式DESC或ASC。

我怎么能坚持在它被排序的方向,无论在页面索引我点击?

谢谢

使用ViewState来存储SortDirection,比如这里或这里。

如果你的BindData方法也加载了它应该加载的数据源,你需要通过这个SortDirection对数据源进行排序。更改BindData,使其接受SortDirection和PageIndex作为参数。

例如:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   this.BindData(this.SortDirection,e.NewPageIndex); //if SortDirection is a property that returns the ViewState value
}

然后排序网格的数据源,并设置相应的PageIndex。

最新更新