【问题标题】:How to keep GridView's sorting state? (Ascending & Descending)如何保持 GridView 的排序状态? (升序降序)
【发布时间】:2011-05-21 17:45:57
【问题描述】:

我有一个问题困扰了我很长一段时间,由于我是 .NET 的初学者,我迫切需要帮助。

我正在使用 GridView 来显示查询结果。但是,我没有将其直接注入实体/LINQ 数据源,而是手动编写了诸如加载、排序和分页之类的事件。问题出在排序中,我无法保持升序/降序状态。我能想到的一个可能的解决方案是缓存状态,但是,我觉得还有另一种更简洁的方法。因此,你们能建议我其他更适合的想法吗?

提前非常感谢! 下面是我用于排序的代码。显然,e.SortDirection 将始终等于ascending,无论我点击了多少次列的标题。

switch (e.SortExpression)
        {
            case "Album":
                if (e.SortDirection == SortDirection.Ascending)
                    _orderedResult = from doc in _result
                                     orderby doc.DocumentAlbum.Name ascending
                                     select doc;
                else
                    _orderedResult = from doc in _result
                                     orderby doc.DocumentAlbum.Name descending
                                     select doc;
                break;
            case "Category":
                if (e.SortDirection == SortDirection.Ascending)
                    _orderedResult = from doc in _result
                                     orderby doc.DocumentCategory.Name ascending
                                     select doc;
                else
                    _orderedResult = from doc in _result
                                     orderby doc.DocumentCategory.Name descending
                                     select doc;
                break;
            case "Title":
                if (e.SortDirection == SortDirection.Ascending)
                    _orderedResult = from doc in _result
                                     orderby doc.Title ascending
                                     select doc;
                else
                    _orderedResult = from doc in _result
                                     orderby doc.Title descending
                                     select doc;
                break;
            case "Description":
                if (e.SortDirection == SortDirection.Ascending)
                    _orderedResult = from doc in _result
                                     orderby doc.Description ascending
                                     select doc;
                else
                    _orderedResult = from doc in _result
                                     orderby doc.Description descending
                                     select doc;
                break;
            case "DateCreated":
                if (e.SortDirection == SortDirection.Ascending)
                    _orderedResult = from doc in _result
                                     orderby doc.DateCreated ascending
                                     select doc;
                else
                    _orderedResult = from doc in _result
                                     orderby doc.DateCreated descending
                                     select doc;
                break;
            case "DateUpdated":
                if (e.SortDirection == SortDirection.Ascending)
                    _orderedResult = from doc in _result
                                     orderby doc.DateUpdated ascending
                                     select doc;
                else
                    _orderedResult = from doc in _result
                                     orderby doc.DateUpdated descending
                                     select doc;
                break;

        }

【问题讨论】:

标签: linq sorting gridview direction


【解决方案1】:

其实,我刚刚找到了答案。我使用 ViewState 函数来跟踪状态。 这是我使用的功能:

private SortDirection GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending
        SortDirection _sortDirection = SortDirection.Ascending;

        // 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 = SortDirection.Descending;
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection"] = _sortDirection.ToString();
        ViewState["SortExpression"] = column;

        return _sortDirection;
    }

【讨论】:

    【解决方案2】:
    protected void gvOfflineSub_Sorting(object sender, GridViewSortEventArgs e)
        {
            IList<SellerDetails> Ilist = gvOfflineSub.DataSource as IList<SellerDetails>;
            DataTable dt = ToDataTable<SellerDetails>(Ilist);
            if (dt != null)
            {
                DataView dataView = new DataView(dt);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
    
                gvOfflineSub.DataSource = dataView;
                gvOfflineSub.DataBind();
            }
        }
    
        /// <summary>
        /// Description:Following Method is for Sorting Gridview.
        /// </summary>
        /// <param name="sortDirection"></param>
        /// <returns></returns>
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;
    
            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;
    
                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 LINQ,那么排序非常简单 像这样绑定网格

      var data = (from d in edc.TableName
                      select new
                      {
                          d.Address,
                          d.InsertedDate,                        
                          d.ID
                      }).OrderByDescending(d => d.ID);
          if (data != null)
          {
              grdList.DataSource = data;
              grdList.DataBind();
          }
      

      【讨论】:

        猜你喜欢
        • 2015-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        • 2017-09-21
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        相关资源
        最近更新 更多