允许按列网格视图排序

本文关键字:排序 视图 网格 许按列 | 更新日期: 2023-09-27 18:35:31

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

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

这里是.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();
            }
        }
    }

这里是.aspx代码(仅限网格视图):

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

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

允许按列网格视图排序

网格视图不会自行排序。需要为 GridView 排序事件添加一些代码并将其连接起来。

首先,将OnSorting事件的名称添加到.aspx页面上的GridView

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

然后,在代码隐藏中添加该事件。此示例还处理更改排序方向 - 用户排序后,他们可能希望反转排序方向,您必须跟踪该方向。

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

你需要定义一个排序melhod,并实现它:

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