C# 对gridview控件进行排序-方向永远不会改变

C# 对gridview控件进行排序-方向永远不会改变,c#,gridview,code-behind,C#,Gridview,Code Behind,即使在看了几个例子之后,我似乎也无法正确理解这一点 我有一段代码,它很高兴地按照升序对我的gridview重新排序: // gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are // clicked. You must remember to add (AllowSorting

即使在看了几个例子之后,我似乎也无法正确理解这一点

我有一段代码,它很高兴地按照升序对我的gridview重新排序:

    // gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are
    //   clicked.  You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side
    protected void gridViewSorting(object sender, GridViewSortEventArgs e)
    {
       DataTable dataTable = GVInactive.DataSource as DataTable;

       if (dataTable != null)
       {
          DataView dataView = new DataView(dataTable);
          string SQL = "[" + e.SortExpression + "] " + ConvertSortDirectionToSql(e.SortDirection);
          dataView.Sort = SQL;

          GVInactive.DataSource = dataView.ToTable();
          GVInactive.DataBind();

       }
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "DESC";
                break;

            case SortDirection.Descending:
                newSortDirection = "ASC";
                break;
        }

        return newSortDirection;
    }
但是,当我第二次单击标题时,它应该会反转之前的排序顺序。从来没有。每次单击列的标题时,它都会点击
大小写排序方向。升序:
行并设置newSortDirection=“DESC”。数据是按降序排序的,但当我再次单击标题时,它会将SortDirection解析为升序


有什么想法吗?

我们使用ViewState变量来存储最新的排序方向。对网格进行排序时,我们将网格的排序条件和排序方向与存储最后一个排序表达式的ViewState变量进行比较。如果列相等,则检查上一次排序的方向和反向排序

例如:

    private string SortCriteria
    {
        get
        {
            if (ViewState["sortCriteria"] == null)
            {
                ViewState["sortCriteria"] = "";
            }

            return ViewState["sortCriteria"].ToString();
        }
        set
        {
            ViewState["sortCriteria"] = value;
        }
    }

    private string SortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = "";
            }

            return ViewState["sortDirection"].ToString();
        }
        set
        {
            ViewState["sortDirection"] = value;
        }
    }

    protected void gvData_Sorting(object sender, GridViewSortEventArgs e)
    {
        gvData.EditIndex = -1;

        if (SortCriteria == e.SortExpression)
        {
            if (SortDirection == string.Empty || SortDirection == "DESC") { SortDirection = "ASC"; }
            else { SortDirection = "DESC"; }
        }
        else
        {
            SortCriteria = e.SortExpression;
            SortDirection = "ASC";
        }

        BindGrid();
    }

    private void BindGrid()
    {
        DataTable dt = new [However you get dataset from database];
        DataView dv = new DataView(dt);
        dv.Sort = string.Format("{0} {1}", SortCriteria, SortDirection).Trim();

        gvData.DataSource = dv;
        gvData.DataBind();
    }

为了记录在案,我用这个替换了我问题中使用的代码,它工作得非常好

    // gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are
    //   clicked.  You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side
    protected void gridViewSorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GVInactive.DataSource as DataTable;
        string sortExpression = e.SortExpression; 
        string direction = string.Empty;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);

            if (SortDirection == SortDirection.Ascending) 
            { 
                SortDirection = SortDirection.Descending; 
                direction = " DESC"; 
            } 
            else 
            { 
                SortDirection = SortDirection.Ascending; 
                direction = " ASC"; 
            }

            DataTable table = GVInactive.DataSource as DataTable;
            table.DefaultView.Sort = sortExpression + direction;

            GVInactive.DataSource = table;
            GVInactive.DataBind();

        }
    }

    public SortDirection SortDirection 
    { 
        get 
        { 
            if (ViewState["SortDirection"] == null) 
            { 
                ViewState["SortDirection"] = SortDirection.Ascending; 
            } 
            return (SortDirection)ViewState["SortDirection"]; 
        } 
        set 
        { 
            ViewState["SortDirection"] = value; 
        } 
    }
我可能可以删除一些行(我甚至不确定我是否需要DataView),但它可以完美地工作。请记住在GridView标记的ASP端添加以下内容:

AllowSorting=“True”OnSorting=“gridViewSorting”


然后在适当的地方更改gridview的名称。

看起来BindGrid()是一个函数。你也能发布这个功能吗;没有它,我只有半个答案。还有,我需要什么特别的参考资料才能工作吗?很好。我现在添加了BindGrid方法,它显示了SortCriteria和SortDirection属性的使用。抱歉疏忽了。