System.Web.UI.WebControls.GridView.AllowSorting Property

Gets or sets a value indicating whether the sorting feature is enabled.

Syntax

[System.ComponentModel.DefaultValue(false)]
public virtual bool AllowSorting { set; get; }

Value

Documentation for this section has not yet been entered.

Remarks

When a data source control that supports sorting is bound to the System.Web.UI.WebControls.GridView control, the System.Web.UI.WebControls.GridView control can take advantage of the data source control's capabilities and provide automatic sorting functionality. When the System.Web.UI.WebControls.GridView control is bound to a data source by setting the BaseDataBoundControl.DataSource property programmatically, you must provide the sorting functionality by using the GridView.Sorting event.

Note:

Different data sources have different requirements for enabling their sorting capabilities. To determine the requirements, see the documentation for the specific data source.

To enable sorting, set the GridView.AllowSorting property to true. When sorting is enabled, the heading text for each column field with its DataControlField.SortExpression property set is displayed as a link button.

Note:

The DataControlField.SortExpression property for an automatically generated columns field is automatically populated. If you define your own columns through the GridView.Columns collection, you must set the DataControlField.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 items in the System.Web.UI.WebControls.GridView control to be sorted based on the sort expression. Typically, the sort expression is simply the name of the field displayed in the column, which causes the System.Web.UI.WebControls.GridView control to sort with respect to that column. To sort by multiple fields, use a sort expression that contains a comma-separated list of field names. You can determine the sort expression that the System.Web.UI.WebControls.GridView control is applying by using the GridView.SortExpression property. Clicking a column's link button repeatedly toggles the sort direction between ascending and descending order. To determine the current sort direction, use the GridView.SortDirection property.

The System.Web.UI.WebControls.GridView control provides several events that you can use to perform a custom action when sorting occurs. The following table lists the available events.

GridView.Sorted

Occurs when the hyperlink to sort a column is clicked, but after the System.Web.UI.WebControls.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.

GridView.Sorting

Occurs when the hyperlink to sort a column is clicked, but before the System.Web.UI.WebControls.GridView control handles the sort operation. This event is often used to cancel the sorting operation or to perform a custom sorting routine.

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0