Skip to main content

Sorting Modes and Custom Sorting

  • 4 minutes to read

The GridControl supports data sorting by cell values and display text. For each column, you can specify how their 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.

Note

Sorting by display text and custom sorting are not supported in Server Mode.

When users modify cell values against which the GridControl is sorted, the grid moves the modified row to a new position according to applied sort options. You can set the DataViewBase.ImmediateUpdateRowPosition property to false to keep row positions until the grid is refreshed.

Sorting by Another Column

The GridControl‘s default sorting and grouping behavior is as follows. On clicking a column header or changing the ColumnBase.SortOrder property, data is sorted by the corresponding field. On grouping against a column, data groups are created based on the values of this column.

The ColumnBase.SortFieldName property allows you to change this behavior. When sorting/grouping is applied to a specific column, you can sort/group data against another field instead. To do this, set the ColumnBase.SortFieldName property to the required field name.

Custom Sorting

You can create custom rules to sort data. To do this, follow the steps below:

  1. Specify the ColumnBase.SortOrder or GridColumn.GroupIndex property.
  2. Set the ColumnBase.SortMode property to Custom.
  3. Create a command that uses custom rules to sort rows.
  4. Assign the command to the GridControl.CustomColumnSortCommand property.

In the command, you need to compare two rows. The FirstValue and SecondValue parameters identify the values of these rows in the FieldName column. 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.

Example: How to Sort Rows Based on Custom Logic

The following example demonstrates how to use custom rules to sort data in the GridControl:

DevExpress WPF | Grid Control - Custom Sort Rules

View Example: How to Use Custom Rules to Sort Data

<dxg:GridControl ItemsSource="{Binding Items}"
                 CustomColumnSortCommand="{Binding CustomColumnSortCommand}">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Day" GroupIndex="0" SortMode="Custom" />
        <dxg:GridColumn FieldName="Employee" />
    </dxg:GridControl.Columns>
</dxg:GridControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
// ...
    [Command]
    public void CustomColumnSort(RowSortArgs args) {
        if(args.FieldName == "Day") {
            int dayIndex1 = GetDayIndex(args.FirstValue);
            int dayIndex2 = GetDayIndex(args.SecondValue);
            args.Result = dayIndex1.CompareTo(dayIndex2);
        }
    }
    int GetDayIndex(object day) {
        return (int)Enum.Parse(typeof(DayOfWeek), (string)day);
    }
}