System.Web.UI.WebControls Namespace GridView Class
.NET Framework version 2.0
Sets or retrieves a value specifying whether sorting is enabled.
Inline |
<asp:gridview allowsorting = true | false ... > |
Script |
GridView.AllowSorting [ = true | false ] |
This property accepts or returns only a boolean value: true if sorting is enabled; otherwise, false. Default value is false.
Use the AllowSorting property to specify or determine whether sorting is enabled or disabled in a GridView control.
When a data source control that supports sorting is bound to the GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic sorting functionality.
To enable sorting, set the AllowSorting property to true. When sorting is enabled in a GridView using default bound fields, LinkButton controls are rendered in the header section of each column, enabling users to set the order of the data in the grid by the selected column.
NOTE: The SortExpression property for automatically generated columns is automatically populated. If you define your own columns through the Columns collection, you must set the SortExpression property for each column; otherwise, the column will not display the link button in the header.
Clicking the link button for a column causes the rows in the GridView control to be sorted based on the sort expression. Clicking a column's link button repeatedly toggles the SortDirection between ascending and descending order.
The GridView control provides several events that you can use to perform a custom action when sorting occurs. The typical logic for a sort handler is to pass the fields or expression to sort by, and then rebind the data to the GridView control. The following table lists the available events.
Event |
Description |
Sorted |
Occurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation. This event is commonly used to perform a task after the user clicks a hyperlink to sort a column. |
Sorting |
Occurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This event is often used to cancel the sorting operation or to perform a custom sorting routine. |
The following example shows using the AllowSorting property to enable sorting in a GridView control when automatically generated columns are used.
<asp:gridview id = "CustomersGridView" runat = "server"
datasourceid = "customers"
width=80% cellpadding=5
font-size = "8pt"
allowsorting>
GridView Members SortExpression SortDirection Adding Sorting to a GridView Control