Back to Devexpress

TreeList.CustomColumnSort Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-457d97c1.md

latest3.4 KB
Original Source

TreeList.CustomColumnSort Event

Allows you to apply custom sorting for those Tree List columns whose TreeListColumn.SortMode is set to Custom.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[DXCategory("Behavior")]
public event CustomColumnSortEventHandler CustomColumnSort
vb
<DXCategory("Behavior")>
Public Event CustomColumnSort As CustomColumnSortEventHandler

Event Data

The CustomColumnSort event's data class is DevExpress.XtraTreeList.CustomColumnSortEventArgs.

Remarks

The CustomColumnSort event fires when a user sorts nodes against a column (clicks a column header or invokes the corresponding command in the column header context menu) or when the TreeListColumn.SortOrder property value changes.

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.

csharp
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;

}
vb
Imports DevExpress.XtraTreeList

Private treeList1.Columns("Department").SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom

Private Sub TreeList1_CustomColumnSort(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomColumnSortEventArgs)
    If e.Node1.HasChildren AndAlso (Not e.Node2.HasChildren) Then
        e.Result = If(e.SortOrder = SortOrder.Ascending, -1, 1)
    End If
    If (Not e.Node1.HasChildren) AndAlso e.Node2.HasChildren Then
        e.Result = If(e.SortOrder = SortOrder.Ascending, 1, -1)
    End If

End Sub

See Also

SortOrder

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace