Skip to main content
All docs
V21.2

GridControl.CustomColumnSortCommand Property

Gets or sets a command that uses custom rules to sort rows.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v21.2.dll

Declaration

public ICommand<RowSortArgs> CustomColumnSortCommand { get; set; }

Property Value

Type Description
DevExpress.Mvvm.ICommand<RowSortArgs>

A command that uses custom rules to sort rows.

Remarks

Bind a command to the CustomColumnSortCommand property to maintain a clean MVVM pattern. The command works like a CustomColumnSort event handler and allows you to specify custom sort operations in a View Model.

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

  1. Set the ColumnBase.SortMode property to Custom.
  2. Create a command that uses custom rules to sort rows.
  3. Assign the command to the 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.

Note

The CustomColumnSortCommand property does not work in Server Mode.

For more information, refer to the following help topic: Sorting Modes and Custom Sorting.

Example

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);
    }
}
See Also