New to Telerik UI for WPF? Download free 30-day trial

Programmatic Sorting

Besides the built-in sorting functionality, you are able to programmatically sort the data in RadGridView using the SortDescriptors collection. This collection of ISortDescriptor objects allows you to use descriptors that define the sorting property (or column) and the sorting direction for the bound data. Because this is a collection, you are able not only to add, but you can also remove or clear the entries.

If you use ICollectionView as data source, RadGridView will automatically synchronize the SortDescriptors of the source with its own ones.

As of Q3 2010, RadGridView adds/removes ColumnSortDescriptor to its SortDescriptors collection when the user sorts from the UI.

You can set the SortMemberPath property of the column to specify the name of the property the data in the column will be sorted by (applies to ColumnSortDescriptor only).

When you add a new descriptor to the collection, the data is automatically sorted according to it. To learn how to create and configure descriptors, look at the following example:

ColumnSortDescriptor csd = new ColumnSortDescriptor() 
{ 
    Column = this.clubsGrid.Columns["Name"], 
    SortDirection = ListSortDirection.Descending 
}; 
this.clubsGrid.SortDescriptors.Add(csd); 
Dim csd As New ColumnSortDescriptor() With { _ 
 .Column = Me.clubsGrid.Columns("Name"), _ 
 .SortDirection = ListSortDirection.Descending _ 
} 
Me.clubsGrid.SortDescriptors.Add(csd) 

FIGURE 1: Programmatically sorted RadGridView: Telerik WPF DataGrid ProgrammaticSorting 1

Another approach is to add the new SortDescriptor object (instead of ColumnSortDescriptor) to the RadGridView.SortDescriptors collection:

SortDescriptor descriptor = new SortDescriptor(); 
descriptor.Member = "Title"; 
descriptor.SortDirection = ListSortDirection.Ascending; 
Dim descriptor As New SortDescriptor() 
descriptor.Member = "Title" 
descriptor.SortDirection = ListSortDirection.Ascending 

The Member property defines the property by which the data will be sorted. The SortDirection property allows you to define the sorting direction.

When SortMemberPath is specified, you should apply ColumnSortDescriptor so that the information from the SortMemberPath is respected.

You can easily create a sort descriptor in XAML and then add it to the SortDescriptors collection. For example:

<telerik:RadGridView x:Name="radGridView" 
             AutoGenerateColumns="False"> 
 
    <telerik:RadGridView.SortDescriptors> 
        <telerik:SortDescriptor Member="Title" 
                        SortDirection="Ascending" /> 
    </telerik:RadGridView.SortDescriptors> 
</telerik:RadGridView> 

As of Q3 2011, you can create a ColumnSortDescriptor in XAML and then add it to the SortDescriptors collection.

For example:

<telerik:RadGridView x:Name="clubsGrid"  
                AutoGenerateColumns="False"> 
    <telerik:RadGridView.Columns> 
 
    </telerik:RadGridView.Columns> 
    <telerik:RadGridView.SortDescriptors> 
        <telerik:ColumnSortDescriptor Column="{Binding Columns[\Title], ElementName=clubsGrid}" SortDirection="Ascending"/> 
    </telerik:RadGridView.SortDescriptors> 
</telerik:RadGridView> 

XAML Tip

In case of a static data structure, known during design time, it is better to declare your default sorting in XAML, rather than in your code-behind.

After the application runs with this descriptor defined, RadGridView data will be sorted ascending by the Title column and will look as if the user clicked on the Title column header.

Adding or removing descriptors from the SortDescriptors collection won't raise Sorting and Sorted events, although the data will be sorted.

The built-in sorting also uses the SortDescriptors collection. When a header is clicked, it clears the SortDescriptors collection and adds a new ColumnSortDescriptor to it.

You are able to add SortDescriptors/ColumnSortDescriptor in XAML only at design time. tip When implementing multi-column sorting behavior you have to manage the SortDescriptors collection at runtime. To learn more, look at the Multi-column Sorting topic.

Clearing the SortDescriptors Collection of the RadGridView

If you need to apply multiple sorting operations and you would like to reset all sorting rules applied, the SortDescriptors collection of the RadGridView should be cleared.

Consider the following scenario. You apply a programmatic sorting for your RadGridView as follows:

ColumnSortDescriptor csd = new ColumnSortDescriptor(); 
clubsGrid.SortDescriptors.Clear(); 
csd.Column = clubsGrid.Columns["Name"]; 
csd.SortDirection = ListSortDirection.Descending; 
clubsGrid.SortDescriptors.Add(csd); 
Dim csd As New ColumnSortDescriptor() 
clubsGrid.SortDescriptors.Clear() 
csd.Column = clubsGrid.Columns("Name") 
csd.SortDirection = ListSortDirection.Descending 
clubsGrid.SortDescriptors.Add(csd) 

At this point, the RadGridView has the following state:

FIGURE 3: Telerik WPF DataGrid ProgrammaticSorting 1

Eventually, you need to sort the grid by another column and would like to remove the previously applied rule. Use the following approach:

ColumnSortDescriptor csd = new ColumnSortDescriptor(); 
clubsGrid.SortDescriptors.Clear(); 
csd.Column = clubsGrid.Columns["Established"]; 
csd.SortDirection = ListSortDirection.Descending; 
clubsGrid.SortDescriptors.Add(csd); 
Dim csd As New ColumnSortDescriptor() 
clubsGrid.SortDescriptors.Clear() 
csd.Column = clubsGrid.Columns("Established") 
csd.SortDirection = ListSortDirection.Descending 
clubsGrid.SortDescriptors.Add(csd) 

The result will be:

FIGURE 4: Telerik WPF DataGrid ProgrammaticSorting 2

See Also

In this article