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.
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 |
---|---|
Column |
Sorts Grid |
Column |
Sorts Grid |
Grid |
Sorts data by the values of the specified column. |
Data |
Clears the sorting applied to the Grid |
Grid |
Occurs before the Grid |
Grid |
Occurs after the Grid |
Note
Group rows are always sorted. The Grid
#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.
Sort Data by Edit Values (Default)
Sorts a column’s data by its edit values. To enable this mode, set the ColumnBase.SortMode property to ColumnSortMode.Value.
Sort Data by Display Text
Sorts a column’s data by its display text (the strings displayed within data cells). To enable this mode, set the ColumnBase.SortMode property to ColumnSortMode.DisplayText.
Custom Sorting
Allows you to apply custom logic to sort data. Handle the GridControl.CustomColumnSort event to do this. To enable this mode, set the ColumnBase.SortMode property to ColumnSortMode.Custom.
#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.
<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);
}