Skip to main content

Sort Data

  • 3 minutes to read

Users can click a column header to sort the GridControl by this column. They can click the header again to change the sort order. The sort glyph indicates the column’s current sort order. To sort the GridControl against multiple columns, users should click column headers with the Shift key pressed. To clear sorting, users can hold the Ctrl key and click the column headers.

DevExpress WinUI Controls | Grid Control - Sort Data

To allow users to sort data against multiple columns without the Shift key pressed, set the DataControlBase.PreserveSorting property to true.

Set the ColumnBase.AllowSorting property to false to disable sorting in the grid column.

You can sort data in code regardless of the AllowSorting property settings.

Sort Data in Code

Use the following methods and properties to sort/unsort data at runtime.

API Description
ColumnBase.SortOrder Sorts GridControl data against the column in the specified order.
ColumnBase.SortIndex Sorts GridControl data against the column in ascending order and specifies the column’s position among sorted columns.
GridControl.SortBy Sorts data by the values of the specified column.
DataControlBase.ClearSorting Clears the sorting applied to the GridControl.
GridControl.StartSorting Occurs before the GridControl sorts its data.
GridControl.EndSorting Occurs after the GridControl sorts its data.

Note

Group rows are always sorted. The GridControl raises the StartSorting and EndSorting events before and after data is grouped.

Sort Modes

You can sort GridControl data by cell values and display text. For each column, you can specify how data should be sorted - by the edit or display values, or using custom logic. To specify the required sort mode, use the ColumnBase.SortMode property.

Custom Row Sort

Set the ColumnBase.SortMode property to Custom and handle the GridControl.CustomColumnSort event to apply custom logic to sort data.

Handle this event to compare two rows. The Value1 and Value2 parameters identify the values of these rows. Set the result of the comparison to the Result parameter as follows:

  • -1 to position the first row above the second row when data is sorted in ascending order. When data is sorted in descending order, the GridControl positions the first row below the second row.
  • 1 to position the first row below the second row when data is sorted in ascending order. When data is sorted in descending order, the GridControl positions the first row above the second row.
  • 0 to indicate that the rows are equal. In this case, the GridControl arranges rows according to their indices in a data source.

Set the Handled parameter to true to use the custom comparison. Leave this parameter set to false to invoke the default comparison mechanism after your event handler has finished. In this instance, the GridControl ignores the custom comparison’s result.

Example: How to Implement Custom Sorting

The following code sample uses custom rules to sort data in the GridControl.

DevExpress WinUI Control | GridControl Custom Column Sort

<dxg:GridControl x:Name="grid" 
                 ItemsSource="{x:Bind ViewModel.Items}" 
                 AutoGenerateColumns="False"
                 CustomColumnSort="grid_CustomColumnSort">
    <dxg:GridControl.Columns>
        <dxg:GridTextColumn FieldName="Day" GroupIndex="0" SortMode="Custom"/>
        <dxg:GridTextColumn FieldName="Employee"/>
    </dxg:GridControl.Columns>
</dxg:GridControl>
void grid_CustomColumnSort(object sender, DevExpress.WinUI.Grid.CustomColumnSortEventArgs e) {
    if(e.Column.FieldName == "Day") {
        int dayIndex1 = GetDayIndex((string)e.Value1);
        int dayIndex2 = GetDayIndex((string)e.Value2);
        e.Result = dayIndex1.CompareTo(dayIndex2);
        e.Handled = true;
    }
}
int GetDayIndex(string day) {
    return (int)Enum.Parse(typeof(DayOfWeek), day);
}
See Also