2

我有一个启用排序的 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();
}
4

2 回答 2

1

您可以使用会话变量来存储最新的排序表达式,并在下次对网格进行排序时将网格的排序表达式与存储上次排序表达式的会话变量进行比较。如果列相等,则检查先前排序的方向并按相反方向排序。

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";
 }
于 2014-02-19T08:55:57.640 回答
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
于 2012-10-22T14:53:01.103 回答