如何对 GridView 进行排序?

发布于 2024-12-11 22:56:22 字数 702 浏览 0 评论 0原文

ASP.NET GridView 排序:我想在单击列名称时将排序方向从升序更改为降序。但默认情况下,排序方向始终显示升序。我是否缺少任何属性设置来切换排序方向?非常感谢任何帮助。谢谢。

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{

    DataTable dataTable = myDataTable(strSQL);

    if (dataTable != null)
    {

        DataView dataView = new DataView(dataTable);

        dataView.Sort = e.SortExpression + " " + SortDirection(e.SortDirection);

        gridView.DataSource = dataView;

        gridView.DataBind();

     }

}
//e.SortDirection always showing ascending

用于排序的aspx gridview列设计:

<asp:BoundField DataField="CREATE_DATE" HeaderText="Create Date" SortExpression="CREATE_DATE" />

ASP.NET GridView sorting: I want to change the sorting direction from ascending to descending when I click the column name. But by default, sorting direction always showing ascending. Am I missing any property setting to toggle sorting direction? Any help greatly appreciated. Thanks.

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{

    DataTable dataTable = myDataTable(strSQL);

    if (dataTable != null)
    {

        DataView dataView = new DataView(dataTable);

        dataView.Sort = e.SortExpression + " " + SortDirection(e.SortDirection);

        gridView.DataSource = dataView;

        gridView.DataBind();

     }

}
//e.SortDirection always showing ascending

aspx gridview column design for sorting:

<asp:BoundField DataField="CREATE_DATE" HeaderText="Create Date" SortExpression="CREATE_DATE" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-12-18 22:56:22

您是否尝试设置 AllowSorting标记中 GridView 属性设置为 true?

<asp:GridView ID="GridView1" runat="server" AllowSorting="True">
    <Columns>
        ...
    </Columns>
</asp:GridView>

或者,您可以在 DataBind 之后的代码中执行此操作:

GridView1.AllowSorting = true;

此属性允许用户单击列标题以对该列进行排序。

编辑: 您实际上必须创建逻辑来决定是升序还是降序排序e.SortDirection 只会如果您使用“数据源控件”(例如SQLDataSource),则会为您提供帮助。然后,您可以将 dataView.Sort 代码更改为如下所示:

if(someCondition)
    dataView.Sort = e.SortExpression + " DESC";
else
    dataView.Sort = e.SortExpression + " ASC";

其中 someCondition 是决定哪种排序方式的逻辑(例如,您可以跟踪该列的最后一个排序方向) ,然后如果用户再次单击它,则执行相反的操作)。

Did you try setting the AllowSorting attribute of the GridView to true in the markup?

<asp:GridView ID="GridView1" runat="server" AllowSorting="True">
    <Columns>
        ...
    </Columns>
</asp:GridView>

Or, you could do it in your code behind after the DataBind:

GridView1.AllowSorting = true;

This attribute allows the user to click on the column headers to sort on that column.

Edit: You actually have to create logic to decide whether to sort ascending or descending (e.SortDirection will only help you if your using a "data source control" like SQLDataSource). Then you can just change your dataView.Sort code to some like this:

if(someCondition)
    dataView.Sort = e.SortExpression + " DESC";
else
    dataView.Sort = e.SortExpression + " ASC";

Where someCondition is your logic for deciding which way to sort (for instance you could keep track of what the last sort direction was for that column, then do the opposite if the user clicks it again).

甜扑 2024-12-18 22:56:22

在绑定数据之前,请按照您想要的方向对其进行排序,然后执行以下操作:

gridview.DataSource=data;
gridview.DataBind();

如果您的数据源是 SQLDataSource 或类似的数据源,只需确保您的 select 语句按照您最初想要的方式进行排序即可。

Before you bind the data, sort it in the direction you want and then do:

gridview.DataSource=data;
gridview.DataBind();

If your datasource is SQLDataSource or something similar, just make sure your select statement does the sorting the way you want initially.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文