Back to Devexpress

GridControl.CustomColumnSort Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-6f492689.md

latest8.0 KB
Original Source

GridControl.CustomColumnSort Event

Enables you to sort data using custom rules.

Namespace : DevExpress.Xpf.Grid

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

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event CustomColumnSortEventHandler CustomColumnSort
vb
Public Event CustomColumnSort As CustomColumnSortEventHandler

Event Data

The CustomColumnSort event's data class is CustomColumnSortEventArgs. The following properties provide information specific to this event:

PropertyDescription
ColumnGets the column whose values are being compared.
HandledGets or sets whether a comparison operation is handled and no default processing is required.
ListSourceRowIndex1Gets the index of the first of the two rows being compared in the data source.
ListSourceRowIndex2Gets the index of the second of the two rows being compared in the data source.
ResultGets or sets the result of a custom comparison.
Row1
Row2
SortOrderGets the sort order applied to the column.
SourceGets the grid control that raised the event.
Value1Gets the first value being compared.
Value2Gets the second value being compared.

Remarks

To sort column values using custom logic, set a column’s ColumnBase.SortMode property to ColumnSortMode.Custom, and handle the CustomColumnSort event.

When this event is fired, two rows should be compared. The column being processed is specified by the Column parameter. The Value1 and Value2 parameters identify the values of the rows within this column. The result of the custom comparison should be set to the Result parameter as follows:

  • -1 if the first row should be positioned above the second row when data is sorted in ascending order. When data is sorted in descending order, the first row will be positioned below the second row.
  • 1 if the first row should be positioned below the second row when data is sorted in ascending order. When data is sorted in descending order, the first row will be positioned above the second row.
  • 0 to indicate that the rows are equal. In this case, the rows will be arranged within a View according to their indices in a data source.

The event parameter’s Handled property should be set to true if the comparison operation was handled. You can leave this parameter set to false , to invoke the default comparison mechanism after your event handler has finished. In this instance, the custom comparison operation’s result is ignored.

Note

The CustomColumnSort event does not work in Server Mode.

If you want to maintain a clean MVVM pattern and specify custom sort operations in a View Model, create a command and bind it to the CustomColumnSortCommand property.

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:

View Example: How to Use Custom Rules to Sort Data

xaml
<dxg:GridControl x:Name="grid"
                 CustomColumnSort="OnCustomColumnSort">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Day" GroupIndex="0" SortMode="Custom"/>
        <dxg:GridColumn FieldName="Employee"/>
    </dxg:GridControl.Columns>
</dxg:GridControl>
csharp
void OnCustomColumnSort(object sender, CustomColumnSortEventArgs e) {
    if(e.Column.FieldName == "Day") {
        int dayIndex1 = GetDayIndex(e.Value1);
        int dayIndex2 = GetDayIndex(e.Value2);
        e.Result = dayIndex1.CompareTo(dayIndex2);
        e.Handled = true;
    }
}
int GetDayIndex(object day) {
    return (int)Enum.Parse(typeof(DayOfWeek), (string)day);
}
vb
Private Sub OnCustomColumnSort(ByVal sender As Object, ByVal e As DevExpress.Xpf.Grid.CustomColumnSortEventArgs)
    If Equals(e.Column.FieldName, "Day") Then
        Dim dayIndex1 As Integer = Me.GetDayIndex(e.Value1)
        Dim dayIndex2 As Integer = Me.GetDayIndex(e.Value2)
        e.Result = dayIndex1.CompareTo(dayIndex2)
        e.Handled = True
    End If
End Sub

Private Function GetDayIndex(ByVal day As Object) As Integer
    Return CInt(System.[Enum].Parse(GetType(System.DayOfWeek), CStr(day)))
End Function

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomColumnSort event.

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_CodeBehind/MainWindow.xaml#L9

xml
<dxg:GridControl x:Name="grid"
                 CustomColumnSort="OnCustomColumnSort">
    <dxg:GridControl.Columns>

wpf-data-grid-implement-custom-sorting/CS/CustomSorting_CodeBehind/obj/Debug/net8.0-windows/MainWindow.g.cs#L91

csharp
#line 9 "..\..\..\MainWindow.xaml"
this.grid.CustomColumnSort += new DevExpress.Xpf.Grid.CustomColumnSortEventHandler(this.OnCustomColumnSort);

wpf-data-grid-implement-custom-sorting/VB/CustomSorting_CodeBehind/obj.NetFX/Debug/MainWindow.g.vb#L90

vb
#ExternalSource("..\..\MainWindow.xaml",9)
AddHandler Me.grid.CustomColumnSort, New DevExpress.Xpf.Grid.CustomColumnSortEventHandler(AddressOf Me.OnCustomColumnSort)

See Also

GridControl Class

GridControl Members

DevExpress.Xpf.Grid Namespace