GridView排序:SortDirection始终升序

我有一个gridview,当用户单击标题时,我需要对其元素进行排序。

它的数据源是一个List对象。


aspx是这样定义的:


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

后面的代码是这样定义的:

第一次加载:


protected void btnSearch_Click(object sender, EventArgs e)

{

    List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();

    this.grdHeader.DataSource = items;

    this.grdHeader.DataBind();

}

当用户点击标题时:


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在升序和降序之间交替。


我做错了什么 ?


回首忆惘然
浏览 941回答 3
3回答

小唯快跑啊

您可以使用会话变量来存储最新的排序表达式,并在下次对网格进行排序时,将网格的排序表达式与存储最后一个排序表达式的Session变量进行比较。如果两列相等,则检查上一个排序的方向,然后按相反的方向排序。例:DataTable sourceTable = GridAttendence.DataSource as DataTable;DataView view = new DataView(sourceTable);string[] sortData = ViewState["sortExpression"].ToString().Trim().Split(' ');if (e.SortExpression == sortData[0]){&nbsp; &nbsp; if (sortData[1] == "ASC")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; view.Sort = e.SortExpression + " " + "DESC";&nbsp; &nbsp; &nbsp; &nbsp; this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; view.Sort = e.SortExpression + " " + "ASC";&nbsp; &nbsp; &nbsp; &nbsp; this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";&nbsp; &nbsp; }}else{&nbsp; &nbsp; view.Sort = e.SortExpression + " " + "ASC";&nbsp; &nbsp; this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";}
打开App,查看更多内容
随时随地看视频慕课网APP