我有一个启用排序的 GridView。对于单页结果,升序和降序都可以正常工作。但是当有多个页面时,单独下降效果很好。asceding 也有效,但是当我单击下一页时,它再次变得未排序。我不知道问题是因为排序方向还是分页。请帮助。以下是代码:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView grid = sender as GridView;
//Retrieve the table from the session object.
DataTable dt = Session["List"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
grid.DataSource = Session["List"];
grid.DataBind();
}
}
private string GetSortDirection(string column)
{
string sortDirection = "ASC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindGridView();
GridView1.DataBind();
}