This documentation is archived and is not being maintained.

GridView.Sort Method

Sorts the GridView control based on the specified sort expression and direction.

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

abstract Sort : 
        sortExpression:string * 
        sortDirection:SortDirection -> unit 
override Sort : 
        sortExpression:string * 
        sortDirection:SortDirection -> unit 

Parameters

sortExpression
Type: System.String
The sort expression with which to sort the GridView control.
sortDirection
Type: System.Web.UI.WebControls.SortDirection
One of the SortDirection values.

ExceptionCondition
HttpException

The GridView control is bound to a data source control, but the DataSourceView that is associated with the data source is a null reference (Nothing in Visual Basic).

Use the Sort method to programmatically sort the GridView control using the specified sort expression and direction. The sort expression specifies the column or columns with which to sort. To sort multiple columns, create a sort expression that contains a comma-separated list of field names. The sort direction indicates whether sorting is performed in ascending or descending order. This method is commonly used when you need to sort the GridView control from outside of the control, such as from a different control on the page. This method is also commonly used to programmatically set a default sort order for the GridView control when it is first rendered. Calling this method also raises the Sorted and Sorting events.

The following example demonstrates how to use the Sort method to programmatically sort the GridView control by multiple columns.

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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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