Export (0) Print
Expand All

GridView.AllowSorting Property

Note: This property is new in the .NET Framework version 2.0.

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

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public:
virtual property bool AllowSorting {
	bool get ();
	void set (bool value);
}
/** @property */
public boolean get_AllowSorting ()

/** @property */
public void set_AllowSorting (boolean value)

public function get AllowSorting () : boolean

public function set AllowSorting (value : boolean)

Property Value

true if the sorting feature is enabled; otherwise, false. The default is false.

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.

NoteNote

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 AllowSorting property to true. When sorting is enabled, the heading text for each column field with its SortExpression property set is displayed as a link button.

NoteNote

The SortExpression property for an automatically generated columns field 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 items in the 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 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 GridView control is applying by using the 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 SortDirection property.

The GridView control provides several events that you can use to perform a custom action when sorting occurs. 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 code example demonstrates how to use the AllowSorting property to enable sorting in a GridView control when automatically generated columns are used.

No code example is currently available or this language may not be supported.

The following code example demonstrates how to use the AllowSorting property to enable sorting in a GridView control when a Columns collection is defined. An image is also programmatically added to the header of the column being sorted to indicate the sort direction. You must provide your own images for this sample to work.

No code example is currently available or this language may not be supported.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft