The RadGridView provides you with a built-in sorting functionality, which allows the user to easily sort the data by one or several columns. The data can be sorted in three ways:
- Ascending
- Descending
- No Sort
The data gets sorted when the header of a column is clicked. When sorted you should see the header of the column highlighted and the appropriate arrow for ascending or descending sorting.
By clicking again on the header the sort direction is changed to descending and on the next click the sorting is cleared. The header goes into his normal state and the arrow disappears.
Disabling Sorting
If you don't want your RadGridView to be sortable, you just have to set the CanUserSortColumns to False:
CopyXAML
<telerik:RadGridView x:Name="radGridView"
CanUserSortColumns="False">
</telerik:RadGridView>If you want to disable sorting only for a particular column, you can set its IsSortable property to False:
CopyXAML
<telerik:GridViewColumn IsSortable="False" />
Events
There are two events that are raised, when the data in the RadGridView is sorted. The first one is the Sorting event and is raised before the data is sorted. The second one is the Sorted event and is raised when the data is sorted.
CopyXAML
<telerik:RadGridView x:Name="radGridView"
Sorting="radGridView_Sorting"
Sorted="radGridView_Sorted" />Via the GridViewSortingEventArgs of the Sorting event you can get the instance of the column that is being sorted (Column), the instance of the RadGridView that owns the column (DataControl), the sorting state (SortingState) and others.
You are also able to cancel the sorting operation by setting the Cancel property to True.
CopyC#
private void radGridView_Sorting( object sender, GridViewSortingEventArgs e )
{
e.Cancel = true;
}
CopyVB.NET
Private Sub radGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortingEventArgs)
e.Cancel = True
End SubTo learn how to use the Sorting event to overwrite the built-in sorting functionality take a look at the Custom Sorting topic.
The Sorted event allows you to get an instance of the column by which the data is sorted via its GridViewSortedEventArgs:
In the event handler you can place some code that has to be executed when the data in the RadGridView gets sorted. For example, try to change the background color of the sorted column:
CopyC#
private GridViewColumn previousColumn;
private void radGridView_Sorted( object sender, GridViewSortedEventArgs e )
{
if ( this.previousColumn != null )
{
this.previousColumn.Background = new SolidColorBrush( Colors.Transparent );
}
e.Column.Background = new SolidColorBrush( Colors.Green );
this.previousColumn = e.Column;
}
CopyVB.NET
Private previousColumn As GridViewColumn
Private Sub radGridView_Sorted(ByVal sender As Object, ByVal e As GridViewSortedEventArgs)
If Me.previousColumn IsNot Nothing Then
Me.previousColumn.Background = New SolidColorBrush(Colors.Transparent)
End If
e.Column.Background = New SolidColorBrush(Colors.Green)
Me.previousColumn = e.Column
End SubIn this example, the previous column field is used to store the currently sorted column, in order to revert its background color when another column is selected. Here is a snapshot of the final result.
Styling the Sorted Header
By editing the template of the header cell you are able to change its overall look and feeling. Making use of the VisualStateManager also allows you to adjust the visual behavior according to the actions of the user - sorted descending or ascending etc. You can also change the visual element that represents the direction of the sorting. To learn more about how to do that take a look at the Templating the Column Headers topic.
See Also