1

I have a problem that has been bugging me for quite some time and I'm in desperate need of help due to I am a beginner in .NET.

I am using a GridView to display the query results. However, instead of injecting it directly to the entity/LINQ data source, I manually code the events such as Load, Sorting and Paging. The problem is in the Sorting, I can't keep the ascending/descending state. One of the possible solutions that I can think of is by Caching the state, however, I feel like there is another way that is more neat. Thus, can you guys suggest me any other ideas that suit better?

Thanks a lot in advance! Below is the code that I use for the sorting. Apparently, e.SortDirection will always equals to ascending no matter how many times I have clicked the Column's Header.

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;

        }
4

3 回答 3

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;
    }
于 2011-05-22T05:16:28.210 回答
0

如果您使用的是 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();
    }
于 2014-07-11T05:41:29.237 回答
0
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;
    }
于 2012-05-23T13:26:48.900 回答