Allow sorting by gridview column

I am writing a project that receives data from a Data Acess Layer and displays it in a GridView. The problem is to allow sorting by column. When I click on the column heading, the following error message occurs:

Exception Details: System.Web.HttpException: GridView "GridView1" Trigger Events Sort that has not been processed.

Here is the .cs code:

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's the .aspx code (gridview only):

 <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 anyone know how to enable sorting by columns?

+4
source share
3 answers

GridView does not sort itself. You need to add the code for the GridView sorting event and enable it .

First, you add the OnSorting event OnSorting to the GridView on the .aspx page:

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

Then you add this event to the code. This example also handles changing the direction of the sort — after sorting the user, they may want to change the direction of the sort, and you should keep track of this.

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

You need to define the melhod collation 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(); } } 
+6
source

Why are you all reinventing the wheel and writing code to handle sorting when all you have to do is?

 AllowSorting="True" 
0
source

Source: https://habr.com/ru/post/1414621/


All Articles