Skip to main content

TreeListView.CustomColumnSort Event

Enables you to sort data using custom rules.

Namespace: DevExpress.Xpf.Grid

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

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public event TreeListCustomColumnSortEventHandler CustomColumnSort

Event Data

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

Property Description
Column Gets the column whose values are being compared.
Handled Gets or sets whether a comparison operation is handled, and no default processing is required.
Node1 Gets the first of the two nodes being compared.
Node2 Gets the second of the two nodes being compared.
Result Gets or sets the result of a custom comparison.
SortOrder Gets the sort order applied to the column.
Value1 Gets the first value being compared.
Value2 Gets 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 nodes should be compared. The column being processed is specified by the TreeListCustomColumnSortEventArgs.Column parameter. The TreeListCustomColumnSortEventArgs.Value1 and TreeListCustomColumnSortEventArgs.Value2 parameters identify the values of the nodes within this column. The result of the custom comparison should be set to the TreeListCustomColumnSortEventArgs.Result parameter as follows:

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

The event parameter’s TreeListCustomColumnSortEventArgs.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

For information about the TreeListView sort specifics, refer to the following help topic: Sort Nodes.

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 code sample demonstrates how to use custom rules to sort data in the GridControl:

<dxg:GridControl ItemsSource="{Binding Source}">
    <dxg:GridControl.View>
        <dxg:TreeListView KeyFieldName="ID" 
                          ParentFieldName="ParentID" 
                          CustomColumnSort="OnCustomColumnSort"/>
    </dxg:GridControl.View>
    <dxg:GridColumn FieldName="Day" IsSmart="True" SortMode="Custom" SortOrder="Ascending"/>
    <dxg:GridColumn FieldName="Employee" IsSmart="True"/>
</dxg:GridControl>
void OnCustomColumnSort(object sender, DevExpress.Xpf.Grid.TreeList.TreeListCustomColumnSortEventArgs 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);
}
See Also