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 DataGrid.
Adding a PropertySortDescriptor
The PropertySortDescriptor
exposes a PropertyName
property that specifies the property by which the data will be sorted. It also exposes a SortOrder
property, which allows for controlling the sorting order. The following examples demonstrate how to set up the DataGrid and add a PropertySortDescriptor
.
Define 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>
Populate the DataGrid with Data
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; }
}
Add a Second PropertySortDescriptor in Code
this.grid.SortDescriptors.Add(new PropertySortDescriptor() { PropertyName = "City", SortOrder = SortOrder.Ascending });