How to sort a column in WPF DataGrid (SfDataGrid)?
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 <kbd>Ctrl</kbd> 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
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. The SortColumnsChanging event occurs while sorting the columns by clicking a column header. The 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 }
Take a moment to peruse the documentation, where you can find about sorting, with code examples.
Please refer this link to know about the essential features of Syncfusion WPF DataGrid.
View WPF DataGrid Sorting Demo in GitHub
Conclusion
I hope you enjoyed learning about how to sort a column in WPF DataGrid (SfDataGrid).
You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!