How To Sort A Column In WPF Datagrid (Sfdatagrid)?
About the sample
This example illustrates how to sort a column in WPF DataGrid (SfDataGrid) by enabling AllowSorting property for SfDataGrid or corresponding column
WPF DataGrid (SfDataGrid) allows you to sort the data against one or more columns either in ascending or descending order. The sorting can be performed by clicking a column header. You can enable/disable the sorting for all the columns in DataGrid by using DataGrid.AllowSorting property. Moreover, you can also enable/disable the sorting for a particular column by using Column.AllowSorting.
<syncfusion:SfDataGrid x:Name="dataGrid"
AllowSorting="True"
ItemsSource="{Binding Orders}">
</syncfusion:SfDataGrid>
Programmatic sorting
You can sort a column in programmatic way by using the DataGrid.SortColumnDescriptions property.
<syncfusion:SfDataGrid.SortColumnDescriptions>
<syncfusion:SortColumnDescription ColumnName="OrderID" SortDirection="Ascending" />
<syncfusion:SortColumnDescription ColumnName="CustomerName" SortDirection="Descending" />
</syncfusion:SfDataGrid.SortColumnDescriptions>
Showing sort order numbers
WPF DataGrid (SfDataGrid) allows you to sort the data against one or more columns by clicking a column header by pressing Ctrl key. Sorting orders to denote the order of sorting performed for columns can be enabled by using the DataGrid.ShowSortNumbers property.
<syncfusion:SfDataGrid x:Name="dataGrid"
AllowSorting="True"
ShowSortNumbers="True"
ItemsSource="{Binding Orders}">
</syncfusion:SfDataGrid>
Tristate sorting
By default, the data is sorted in ascending or descending order when clicking a column header. Data can be rearranged to its initial order from descending, when clicking column header by setting SfDataGrid.AllowTriStateSorting property
Following are the sequence of sorting orders when clicking column header,
- Sorts the data in ascending order
- Sorts the data in descending order
- Clears the sorting and records displayed in its initial order
<syncfusion:SfDataGrid x:Name="dataGrid"
AllowSorting="True"
AllowTriStateSorting="True"
ItemsSource="{Binding Orders}">
</syncfusion:SfDataGrid>
Custom Sorting
Columns can be sorted based on the custom logic. The custom sorting can be applied by adding the SortComparer instance to SfDataGrid.SortComparers.
You can go through this user guide to know more about custom sorting.
Events
You can change the sorting behavior using SfDataGrid.SortColumnsChanging and SfDataGrid.SortColumnsChanged event in DataGrid. SortColumnsChanging event occurs while sorting the columns by clicking a column header. SortColumnsChanged event occurs when the sorting is applied to the column.
dataGrid.SortColumnsChanging += DataGrid_SortColumnsChanging;
private void DataGrid_SortColumnsChanging(object sender, Syncfusion.UI.Xaml.Grid.GridSortColumnsChangingEventArgs e)
{
// Do your customization here
}
dataGrid.SortColumnsChanged += DataGrid_SortColumnsChanged;
private void DataGrid_SortColumnsChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSortColumnsChangedEventArgs e)
{
// Do your customization here
}
KB article - How to sort a column in WPF DataGrid (SfDataGrid)?
Requirements to run the demo
Visual Studio 2015 and above versions