ASPxTreeList.CustomNodeSort Event
Enables you to sort data using custom rules.
Namespace: DevExpress.Web.ASPxTreeList
Assembly: DevExpress.Web.ASPxTreeList.v24.1.dll
NuGet Package: DevExpress.Web
Declaration
Event Data
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. |
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:
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;
}