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

Use generic SortDescriptor

This help article will show you how to create generic expression SortDescriptor<T> for a databound RadGridView.

This article will use the viewmodel and model defined in Examples 3 and 4 in the Getting Started article. Before continuing, you should familiarize yourself with it.

Generic SortDescriptor

The RadGridView control allows for ordering the data items by the result of a complex calculation without having to expose it through a read-only property. All you need to do is to use a SortDescriptor<TElement, TKey=>. In order to demonstrate this, we will setup the RadGridView as demonstrated in Example 1.

Example 1: Setting up the RadGridView

<Grid> 
    <Grid.DataContext> 
        <my:MyViewModel /> 
    </Grid.DataContext> 
    <Grid.RowDefinitions> 
        <RowDefinition Height="*" /> 
        <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
 
    <telerik:RadGridView Name="clubsGrid"  
                         ItemsSource="{Binding Clubs}" 
                         AutoGenerateColumns="False" 
                         GroupRenderMode="Flat"> 
        <telerik:RadGridView.Columns> 
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"  /> 
            <telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}"  
                                        Header="Stadium"  
                                        DataFormatString="{}{0:N0}"/> 
        </telerik:RadGridView.Columns> 
    </telerik:RadGridView> 
 
    <StackPanel Grid.Row="1"> 
        <Button Content="Add Generic SortDescriptor" Click="Button_Click" /> 
    </StackPanel> 
</Grid> 
After that, when the button is clicked we will create a generic SortDescriptor and add it to the SortDescriptors collection of the RadGridView. Note, that in Example 2 we are simply returning a property of the bound object, however we have the option of working with all of its properties in order to construct a SortingExpression.

Example 2: Create a generic SortDescriptor

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var descriptor = new SortDescriptor<Club, int> 
    { 
        SortingExpression = c => c.StadiumCapacity, 
        SortDirection = ListSortDirection.Descending 
    }; 
 
    this.clubsGrid.SortDescriptors.Add(descriptor); 
} 

For more information you can check the Sorting section.

See Also

In this article