Skip to main content

Sorting via Code

  • 2 minutes to read

Use the following approaches to sort data in code:

  • The TreeListColumn.SortOrder property sorts data recursively, starting from root nodes.

    treeList1.Columns["Department"].SortOrder = SortOrder.Ascending;
    
  • The TreeList.Sort and TreeListNode.Sort methods allow you to sort data recursively and non-recursively. For instance, you can only sort root nodes, without affecting the order of nested nodes.

    treeList1.OptionsCustomization.AllowSort = false;
    treeList1.Sort(null, treeList1.Columns["DEPARTMENT"], SortOrder.Ascending, false);
    

Clear Sorting

To remove sorting against a column, set its TreeListColumn.SortOrder property to None, or call the Sort method with the SortOrder.None value as the order parameter.

To clear the sort settings for all columns, use the TreeList.ClearSorting method.

Avoid Superfluous Updates

Once you have changed the column’s sort order, the Tree List rearranges and repaints nodes. If you sort by multiple columns, the Tree List rearranges nodes multiple times. You can avoid superfluous updates via the TreeList.BeginSort and TreeList.EndSort methods.

The following code sorts data against the Department and Budget columns. The TreeList.BeginSort and TreeList.EndSort methods wrap the code to avoid superfluous updates.

using DevExpress.XtraTreeList.Columns;
//...
treeList1.BeginSort();
treeList1.Columns["Department"].SortOrder = SortOrder.Ascending;
treeList1.Columns["Budget"].SortOrder = SortOrder.Descending;
treeList1.EndSort();

Sort Columns

The code above sorts data against the “Department” column, and then against the “Budget” column. To explicitly specify the order of sort columns, use the TreeListColumn.SortIndex property.

The TreeList.GetSortColumn and TreeList.SortedColumnCount members allow you to retrieve sort columns.

Sort Events

When a column’s sort settings are changed, the Tree List does the following:

You can handle these events to perform custom actions.