【问题标题】:How to Sort in GridView如何在 GridView 中排序
【发布时间】:2012-11-15 15:56:11
【问题描述】:

这是我的 GV。

<asp:GridView ID="Grid1" runat="server" AutoGenerateColumns="false"
            AllowPaging="True" OnPageIndexChanging="Grid1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="One" HeaderText="One" />
                <asp:BoundField DataField="Two" HeaderText="Two" />
                <asp:BoundField DataField="Three" HeaderText="Three" />
            </Columns>
</asp:GridView>

我正在使用存储过程填充 GV。

table = PublicClass.Sql_GetTable("usp_GetReportGridView", "NCIU");
Grid1.DataSource = table;
Grid1.DataBind();

如何使用列标题进行排序?

【问题讨论】:

    标签: asp.net sorting gridview


    【解决方案1】:

    首先您需要启用 AllowSorting 属性为真。启用后,网格会在每列的标题中呈现 LinkBut​​ton 控件。单击按钮时,将引发网格的 SortCommand 事件。由您在代码中处理此事件。因为DataGrid总是按照它在数据源中出现的顺序来显示数据,典型的逻辑是先对数据源进行排序,然后再将数据重新绑定到网格中。看下面的代码:

    //AllowSorting="True"
    //OnSorting="GridView2_Sorting"
    //SortExpression="name"
    
    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
      //to check whether to display in ascending order or descending order
      if (e.SortExpression.Trim() == this.SortField)
        this.SortDirection = (this.SortDirection == "D" ? "A" : "D");
      else
        this.SortDirection = "A";
      this.SortField = e.SortExpression;
      TempTable();
    }
    

    【讨论】:

    • 很好的答案(而且比我的要好得多,我已经删除了)。
    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    相关资源
    最近更新 更多