排序gridview控件 - 方向永远不会改变

时间:2014-08-27 18:58:18

标签: 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="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;
    }

然而,第二次点击标题时,它应该反转先前的排序顺序。它永远不会。每次单击列标题时,它都会点击case SortDirection.Ascending:行并设置newSortDirection =“DESC”。数据按降序排序,但当我再次单击标题时,它将SortDirection解析为Ascending。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我们使用ViewState变量来存储最新的Sort Direction。在对网格进行排序时,我们将网格的排序标准和排序方向与存储最后排序表达式的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();
    }

答案 1 :(得分:0)

为了记录,我用这个替换了我的问题中使用的代码,它完全

    // 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 ="真" OnSorting =" gridViewSorting"

然后在适当的时候更改您的gridview的名称。