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

PropertySortDescriptor

You can sort the data in a DataGrid by a property value from the class that defines your objects. This can be achieved by adding a PropertySortDescriptor object to the SortDescriptors collection of the RadDataGrid.

Adding a PropertySortDescriptor

The PropertySortDescriptor exposes a PropertyName property that specifies the property by which the data should be sorted. It also exposes a SortOrder property, which allows for controlling the sorting order. Examples 1, 2 and 3 demonstrate how to setup the RadDataGrid and add a PropertySortDescriptor.

Example 1: Defining the RadDataGrid in xaml

<Grid xmlns:grid="using:Telerik.UI.Xaml.Controls.Grid" 
    xmlns:dataCore="using:Telerik.Data.Core"> 
    <grid:RadDataGrid Width="600" Height="460" x:Name="grid"> 
        <grid:RadDataGrid.SortDescriptors> 
            <dataCore:PropertySortDescriptor PropertyName="Country" SortOrder="Ascending"/> 
        </grid:RadDataGrid.SortDescriptors> 
    </grid:RadDataGrid> 
</Grid> 

Example 2: Populating the RadDataGrid

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
        this.InitializeComponent(); 
 
        List<CustomData> data = new List<CustomData> 
        { 
            new CustomData { Country = "Brazil", City = "Caxias do Sul"}, 
            new CustomData { Country = "Ghana", City = "Wa"}, 
            new CustomData { Country = "Brazil", City = "Fortaleza"}, 
            new CustomData { Country = "Italy", City = "Florence" }, 
            new CustomData { Country = "France", City = "Bordeaux" }, 
            new CustomData { Country = "Bulgaria", City = "Vratsa"}, 
            new CustomData { Country = "Spain", City = "Las Palmas"}, 
            new CustomData { Country = "France", City = "Le Mans" }, 
            new CustomData { Country = "Brazil", City = "Santos"}, 
            new CustomData { Country = "Ghana", City = "Ho"}, 
            new CustomData { Country = "Spain", City = "Malaga"}, 
            new CustomData { Country = "France", City = "Marseille" }, 
            new CustomData { Country = "Bulgaria", City = "Koynare" }, 
            new CustomData { Country = "Spain", City = "Valencia"}, 
            new CustomData { Country = "Ghana", City = "Kade" }, 
            new CustomData { Country = "Brazil", City = "Porto Alegre" }, 
            new CustomData { Country = "Bulgaria", City = "Byala Slatina"}, 
            new CustomData { Country = "Italy", City = "Naples" }, 
            new CustomData { Country = "Brazil", City = "Joinville" }, 
        }; 
        this.grid.ItemsSource = data; 
    } 
} 
 
public class CustomData 
{ 
    public string City { get; set; } 
    public string Country { get; set; } 
} 

Figure 1: Result from Examples 1 and 2

Property Sort Descriptor Example

Example 3: Adding a second PropertySortDescriptor in code

this.grid.SortDescriptors.Add(new PropertySortDescriptor() { PropertyName = "City", SortOrder = SortOrder.Ascending }); 

See Also

In this article
Not finding the help you need?