aspnet-devexpress-dot-web-dot-aspxtreelist-dot-aspxtreelist-3e9b7bad.md
Enables you to sort data using custom rules.
Namespace : DevExpress.Web.ASPxTreeList
Assembly : DevExpress.Web.ASPxTreeList.v25.2.dll
NuGet Package : DevExpress.Web
public event TreeListCustomNodeSortEventHandler CustomNodeSort
Public Event CustomNodeSort As TreeListCustomNodeSortEventHandler
The CustomNodeSort event's data class is TreeListCustomNodeSortEventArgs. 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 therefore no default processing is required. |
| Node1 | Gets the first node whose value is being compared. |
| Node2 | Gets the second node whose value is being compared. |
| Result | Gets or sets the result of a custom comparison. |
| SortOrder | Gets the sort order applied to the column being processed. |
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:
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.
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:
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;
}
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