Skip to main content
A newer version of this page is available. .

TreeList.CompareNodeValues Event

Enables you to implement custom sorting for columns whose TreeListColumn.SortMode property is set to Custom. For versions 17.1 and higher, the CompareNodeValues event is deprecated and replaced with the TreeList.CustomColumnSort event.

Namespace: DevExpress.XtraTreeList

Assembly: DevExpress.XtraTreeList.v19.1.dll

Declaration

[DXCategory("Behavior")]
[Browsable(false)]
public event CompareNodeValuesEventHandler CompareNodeValues

Event Data

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

Property Description
Column Gets the column against whose values sorting is performed.
Node1 Gets the node containing the first value involved in comparison.
Node2 Gets the node containing the second value involved in comparison.
NodeValue1 Gets the first value involved in comparison.
NodeValue2 Gets the second value involved in comparison.
Result Gets or sets a comparison result.
SortOrder Gets the sort order applied to the column whose values are going to be compared.

Remarks

In most cases, you will have no need to change the sorting algorithm provided by the control. However, there can be situations when you need to implement custom sorting. For instance, the string “Value10“ is considered “less” than “Value2“, so you may want to sort such kinds of data in a different manner. You may also want to sort nodes depending on their properties. For example, nodes that have children can be arranged above or below nodes that don’t have children.

To use a custom sorting algorithm for a column, set the column’s TreeListColumn.SortMode property to Custom and handle the CompareNodeValues event to implement custom sorting logic. The event’s functionality is based upon comparing all nodes in pairs. Thus, this event allows you to specify which node must be positioned above or below another node when sorting is applied.

Parameters transmitted to the CompareNodeValues event specify the column against which sorting is about to be performed and the sort order applied. Furthermore, values to be compared (together with nodes containing them) are also passed to the event. The result of the comparison must be specified using the CompareNodeValuesEventArgs.Result parameter.

Example

The code below implements a custom sorting algorithm for a Tree List column via the TreeList.CustomColumnSort event. The sorting procedure arranges nodes that have children above the nodes that do not have children.

The images below display the control sorted by the Department column in ascending and descending order. Notice that the Finance node is always located under nodes that have children.

Sorting - Custom

using DevExpress.XtraTreeList;

treeList1.Columns["Department"].SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;

private void TreeList1_CustomColumnSort(object sender, DevExpress.XtraTreeList.CustomColumnSortEventArgs e) {
    if (e.Node1.HasChildren && !e.Node2.HasChildren)
        e.Result = e.SortOrder == SortOrder.Ascending ? -1 : 1;
    if (!e.Node1.HasChildren && e.Node2.HasChildren)
        e.Result = e.SortOrder == SortOrder.Ascending ? 1 : -1;

}
See Also