首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >GridView排序: SortDirection始终升序

GridView排序: SortDirection始终升序
EN

Stack Overflow用户
提问于 2008-10-30 12:41:01
回答 22查看 134.8K关注 0票数 70

我有一个网格视图,当用户单击标题时,我需要对它的元素进行排序。

它的数据源是一个列表对象。

aspx是这样定义的:

代码语言:javascript
复制
<asp:GridView ID="grdHeader" AllowSorting="true" AllowPaging="false" 
    AutoGenerateColumns="false" Width="780" runat="server"  OnSorting="grdHeader_OnSorting" EnableViewState="true">
    <Columns>
        <asp:BoundField DataField="Entitycode" HeaderText="Entity" SortExpression="Entitycode" />
        <asp:BoundField DataField="Statusname" HeaderText="Status" SortExpression="Statusname" />
        <asp:BoundField DataField="Username" HeaderText="User" SortExpression="Username" />
    </Columns>
</asp:GridView>

后台代码是这样定义的:

第一次加载:

代码语言:javascript
复制
protected void btnSearch_Click(object sender, EventArgs e)
{
    List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
    this.grdHeader.DataSource = items;
    this.grdHeader.DataBind();
}

当用户点击标题时:

代码语言:javascript
复制
protected void grdHeader_OnSorting(object sender, GridViewSortEventArgs e)
{
    List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
    items.Sort(new Helpers.GenericComparer<V_ReportPeriodStatusEntity>(e.SortExpression, e.SortDirection));
    grdHeader.DataSource = items;
    grdHeader.DataBind();
}

我的问题是e.SortDirection总是设置为升序。

我有一个类似的代码的网页,它的工作很好,e.SortDirection之间的升降交替。

我做错了什么?

EN

回答 22

Stack Overflow用户

发布于 2009-08-19 07:37:57

会话和视图状态的问题是,如果页面上有多个网格视图,则还必须跟踪存储SortColumn和方向的网格视图控件。

Session和Viewstate的替代方法是向Gridview添加2个属性,并以这种方式跟踪列和方向。

下面是一个示例:

代码语言:javascript
复制
private void GridViewSortDirection(GridView g, GridViewSortEventArgs e, out SortDirection d, out string f)
{
    f = e.SortExpression;
    d = e.SortDirection;

    //Check if GridView control has required Attributes
    if (g.Attributes["CurrentSortField"] != null && g.Attributes["CurrentSortDir"] != null)
    {
        if (f == g.Attributes["CurrentSortField"])
        {
            d = SortDirection.Descending;
            if (g.Attributes["CurrentSortDir"] == "ASC")
            {
                d = SortDirection.Ascending;
            }
        }

        g.Attributes["CurrentSortField"] = f;
        g.Attributes["CurrentSortDir"] = (d == SortDirection.Ascending ? "DESC" : "ASC");
    }

}
票数 52
EN

Stack Overflow用户

发布于 2008-12-30 09:45:30

自动双向排序仅适用于SQL数据源。不幸的是,MSDN中的所有文档都假定您正在使用它,因此GridView可能会有点令人沮丧。

我这样做的方式是自己跟踪订单。例如:

代码语言:javascript
复制
    protected void OnSortingResults(object sender, GridViewSortEventArgs e)
    {
        // If we're toggling sort on the same column, we simply toggle the direction. Otherwise, ASC it is.
        // e.SortDirection is useless and unreliable (only works with SQL data source).
        if (_sortBy == e.SortExpression)
            _sortDirection = _sortDirection == SortDirection.Descending ? SortDirection.Ascending : SortDirection.Descending;
        else
            _sortDirection = SortDirection.Ascending;

        _sortBy = e.SortExpression;

        BindResults();
    }
票数 20
EN

Stack Overflow用户

发布于 2009-02-11 12:54:21

不仅SQL数据源没有这个问题,对象数据源也没有这个问题。但是,当在代码中动态设置DataSource时,就会出现问题。不幸的是,MSDN有时在信息方面真的很差。简单地提到这个行为(这不是一个bug,而是一个设计问题)将会节省大量的时间。无论如何,我不太倾向于使用会话变量来实现这一点。我通常将排序方向存储在ViewState中。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/250037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档