Back to Devexpress

ASPxTreeList.CustomNodeSort Event

aspnet-devexpress-dot-web-dot-aspxtreelist-dot-aspxtreelist-3e9b7bad.md

latest5.9 KB
Original Source

ASPxTreeList.CustomNodeSort Event

Enables you to sort data using custom rules.

Namespace : DevExpress.Web.ASPxTreeList

Assembly : DevExpress.Web.ASPxTreeList.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event TreeListCustomNodeSortEventHandler CustomNodeSort
vb
Public Event CustomNodeSort As TreeListCustomNodeSortEventHandler

Event Data

The CustomNodeSort event's data class is TreeListCustomNodeSortEventArgs. 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 therefore no default processing is required.
Node1Gets the first node whose value is being compared.
Node2Gets the second node whose value is being compared.
ResultGets or sets the result of a custom comparison.
SortOrderGets the sort order applied to the column being processed.

Remarks

The CustomNodeSort event is raised when sorting is applied to a column, and allows a custom sorting algorithm to be implemented.

Nodes are sorted with respect to their nesting levels in order to preserve the tree-like structure.

When the CustomNodeSort event is raised, two nodes are compared. The processed column is specified by the TreeListCustomNodeSortEventArgs.Column property. The TreeListCustomNodeSortEventArgs.Node1 and TreeListCustomNodeSortEventArgs.Node2 properties identify the node values within this column.

The result of the custom comparison should be set to the TreeListCustomNodeSortEventArgs.Result property as shown below:

  • set Result to -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 row.
  • set Result to 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 row.
  • set Result to 0 to indicate that the nodes are equal. In this case, the nodes will be arranged within the tree list according to their indexes in a data source.

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

Example

The following code shows how to implement custom node sorting by handling the ASPxTreeList.CustomNodeSort event. The “DEPARTMENT” column displays text values. When sorting is applied to this column, the nodes are compared by the length of the “DEPARTMENT” column’s values rather than by the text itself.

The image below shows the result:

csharp
using System.Collections;

protected void ASPxTreeList1_CustomNodeSort(object sender,
DevExpress.Web.ASPxTreeList.TreeListCustomNodeSortEventArgs e) {
    if (e.Column.FieldName != "DEPARTMENT") return;
    e.Handled = true;
    string value1 = e.Node1["DEPARTMENT"].ToString();
    string value2 = e.Node2["DEPARTMENT"].ToString();
    if (value1.Length > value2.Length)
        e.Result = 1;
    else
        if (value1.Length == value2.Length)
            e.Result = Comparer.Default.Compare(value1, value2);
        else
            e.Result = -1;
}
vb
Imports System.Collections

Protected Sub ASPxTreeList1_CustomNodeSort(ByVal sender As Object,_
ByVal e As DevExpress.Web.ASPxTreeList.TreeListCustomNodeSortEventArgs)
        If e.Column.FieldName <> "DEPARTMENT" Then
            Return
        End If
        e.Handled = True
        Dim value1 As String = e.Node1("DEPARTMENT").ToString()
        Dim value2 As String = e.Node2("DEPARTMENT").ToString()
        If value1.Length > value2.Length Then
            e.Result = 1
        Else
            If value1.Length = value2.Length Then
                e.Result = Comparer.Default.Compare(value1, value2)
            Else
                e.Result = -1
            End If
        End If
End Sub

See Also

Sort Tree List Data in Code

Tree List

ASPxTreeList Class

ASPxTreeList Members

DevExpress.Web.ASPxTreeList Namespace