我有一个启用了排序的GridView。对于单页结果,升序和降序都可以正常工作。但是当有多个页面时,单独下降就可以了。提升也有效但当我点击下一页时,它再次变得无效。 我不知道问题是由于排序方向还是分页。请帮助。 以下是代码:
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();
}
答案 0 :(得分:1)
您可以使用会话变量来存储最新的Sort Expression,并在下次对网格进行排序时将网格的排序表达式与存储最后一个排序表达式的Session变量进行比较。如果列相等,则检查先前排序的方向,并按相反方向排序。
DataTable sourceTable = GridAttendence.DataSource as DataTable;
DataView view = new DataView(sourceTable);
string[] sortData = Session["sortExpression"].ToString().Trim().Split(' ');
if (e.SortExpression == sortData[0])
{
if (sortData[1] == "ASC")
{
view.Sort = e.SortExpression + " " + "DESC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
答案 1 :(得分:0)
经过这么多研究,我找到了自己问题的正确答案。这可能会在将来帮助某人。将以下代码添加到bindGridView()
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;
和
#region Sorting
protected void Gridview1_Sort(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression + " " + GetSortDirection(e.SortExpression);
Gridview1.DataSource = bindGridView();
Gridview1.DataBind();
}
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
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;
}
#endregion