wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-eb74ba15.md
Gets or sets a command that uses custom rules to sort rows.
Namespace : DevExpress.Xpf.Grid
Assembly : DevExpress.Xpf.Grid.v25.2.dll
NuGet Package : DevExpress.Wpf.Grid.Core
public ICommand<RowSortArgs> CustomColumnSortCommand { get; set; }
Public Property CustomColumnSortCommand As ICommand(Of RowSortArgs)
| Type | Description |
|---|---|
| ICommand<RowSortArgs> |
A command that uses custom rules to sort rows.
|
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:
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.
The following example uses custom rules to sort data in the GridControl:
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);
}
}
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm.Xpf
' ...
Public Class MainViewModel
Inherits ViewModelBase
' ...
<Command>
Public Sub CustomColumnSort(ByVal args As RowSortArgs)
If args.FieldName = "Day" Then
Dim dayIndex1 As Integer = GetDayIndex(args.FirstValue)
Dim dayIndex2 As Integer = GetDayIndex(args.SecondValue)
args.Result = dayIndex1.CompareTo(dayIndex2)
End If
End Sub
Private Function GetDayIndex(ByVal day As Object) As Integer
Return DirectCast(System.Enum.Parse(GetType(DayOfWeek), DirectCast(day, String)), Integer)
End Function
End Class
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomColumnSortCommand property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
wpf-data-grid-implement-custom-sorting/CS/CustomSorting_MVVM/MainWindow.xaml#L12
<dxg:GridControl ItemsSource="{Binding Items}"
CustomColumnSortCommand="{Binding CustomColumnSortCommand}">
<dxg:GridControl.Columns>
See Also