C# 允许按列 gridview 排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10776372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:12:55  来源:igfitidea点击:

allow sorting by column gridview

c#asp.netsortinggridview

提问by Nurlan

I am writing project which gets data from Data Acess Layer and show it in GridView. The problem is to allow sorting by column. When I click on head of columnt following occurs following error:

我正在编写从数据访问层获取数据并在 GridView 中显示的项目。问题是允许按列排序。当我单击列的头部时,会出现以下错误:

Exception Details: System.Web.HttpException: GridView 'GridView1' Trigger Events Sorting, which has not been processed.

异常详细信息:System.Web.HttpException: GridView 'GridView1' 触发事件排序,尚未处理。

Here the .cs code:

这里的 .cs 代码:

public partial class Default: System.Web.UI.Page
    {
        EmployeesTableAdapter eta = new EmployeesTableAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = eta.GetData();
                GridView1.DataBind();
            }
        }
    }

Here the .aspx code(only gridview):

这里的 .aspx 代码(仅限 gridview):

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" 
        AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" />

<Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>                
                    <asp:CheckBox ID="CheckBox1" runat="server">
                </asp:CheckBox>
                </ItemTemplate>                
            </asp:TemplateField>  

            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
            <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

            <asp:TemplateField HeaderText="View">
                <ItemTemplate>                
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/view.png"/>
                </ItemTemplate>                
            </asp:TemplateField> 

            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>                
                    <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="images/edit.png"/>
                </ItemTemplate>
            </asp:TemplateField> 

        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
       </asp:GridView>

Does someony know how to allow sorting by columns?

有人知道如何允许按列排序吗?

回答by jfoliveira

You need to define a sort melhod, and implement it:

您需要定义一个排序方法,并实现它:

<asp:GridView ID="GridView1" **OnSorting="gridViewSorting"** runat="server" />



protected void gridViewSorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as DataTable;

   if (dataTable != null)
   {
      DataView dataView = new DataView(dataTable);
      dataView.Sort = your sort expression

      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}

回答by DOK

The GridView does not sort itself. You need to add some code for a GridView sorting event and wire it up.

GridView 不对自身进行排序。您需要为 GridView 排序事件添加一些代码并将其连接起来

First, you add the name of the OnSortingevent to the GridViewon the .aspxpage:

首先,您添加的名称OnSorting事件到GridView.aspx页:

<asp:GridView ID="gridView" OnSorting="gridView_Sorting" runat="server" />

Then, you add that event in the code-behind. This examplealso handles changing the sort direction -- once the user has sorted, they may want to reverse the sort direction, and you have to keep track of that.

然后,在代码隐藏中添加该事件。此示例还处理更改排序方向——一旦用户排序,他们可能想要反转排序方向,而您必须对此进行跟踪。

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

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as DataTable;

   if (dataTable != null)
   {
      DataView dataView = new DataView(dataTable);
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}