We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to sort a column in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid
Tags: wpf, sorting, datagrid

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>

 

WPF DataGrid Sorting

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>

 

WPF DataGrid Sorting Order

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>

 

WPF DataGrid TriState Sorting

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

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile