【问题标题】:Sorting a gridview control - The direction never changes对 gridview 控件进行排序 - 方向永远不会改变
【发布时间】:2014-10-21 12:02:29
【问题描述】:

即使看了几个例子,我似乎也无法理解这一点。

我有这段代码,它很高兴按升序重新排序我的网格视图:

    // 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 解析为升序。

有什么想法吗?

【问题讨论】:

    标签: c# gridview code-behind


    【解决方案1】:

    我们使用 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();
        }
    

    【讨论】:

    • 看起来 BindGrid() 是一个函数。您也可以发布该功能吗?没有它,我只有一半的答案。另外,我是否需要任何特殊参考才能使其工作?
    • 好点。我现在添加了 BindGrid 方法,该方法显示了 SortCriteria 和 SortDirection 属性的使用。很抱歉疏忽了。
    【解决方案2】:

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

        // 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"

    然后在适当的地方更改您的网格视图的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多