Flexible TreeView Flexible TreeView


Support & Community

Sorting

Previous Table of Contents Next

Flexible TreeView supports flexible nodes sorting without additional programming.
Supporting sorting modes:

  • By columns and node controls` content in the treeview with columns;
  • By node controls` content in the treeview without columns. 



Sorting by columns and node controls` content

If the treeview contains columns, then sorting will follow these rules:

  1. For every column with a SortOrder property not equal to eSortOrder.None, get node controls list with enabled Sortable property;
  2. Get those node controls` content and sort in the order defined in the column`s SortOrder property. 

Automatic re–sorting will take place when:

  • Column`s SortOrder property has changed;
  • Treeview has changed (node or node control added or removed, etc.) and the Options.Sorting.AutoReSort treeview property is enabled. 



Sorting by node controls` content

If the treeview doesn`t contain columns, then sorting will follow these rules:

  1. Get node controls list with enabled Sortable property;
  2. Get those node controls` content and sort them in the ascending order. To change sort order, call the Node.Sort method manually with custom sort order. 

Automatic re–sorting will take place when:

  • Treeview has changed (node or node control added or removed, etc.) and the Options.Sorting.AutoReSort treeview property is enabled. 



Custom sorting

Flexible TreeView allows two ways of custom sorting:

  1. Changing default nodes compare algorithm while sorting algorithm isn`t affected;
  2. Completely manual implementation of sorting algorithm. 

Changing default nodes compare algorithm

When the treeview is sorting nodes, it uses a callback method, defined in the treeview`s DefaultNodeComparer property, that is intended to compare two nodes. You can change it to your implementation to change nodes compare logic.

Example:

tree.DefaultNodeComparer = MyComparer;
tree.ReSort();
 
// new node comparer where we move all checked nodes on top.
eCompareResult MyComparer(Node pNode1, Node pNode2, SortOrder pSortOrder, SortContext pContext)
{
  bool asc;
 
  if(pNode1.CheckState == pNode2.CheckState)
    return eCompareResult.Equal;
 
  asc = pSortOrder == SortOrder.Ascending;
  // move all checked nodes on top.
  if(pNode1.CheckState == eCheckState.Checked)
    return asc ? eCompareResult.Higher : eCompareResult.Lower;
  if(pNode2.CheckState == eCheckState.Checked)
    return asc ? eCompareResult.Lower : eCompareResult.Higher;
 
  // return default comparer`s result.
  return pContext.DefaultComparer(pNode1, pNode2, pSortOrder, pContext);
}


Completely manual implementation of sorting algorithm

Flexible TreeView allows you to completely replace the default sorting logic, allowing a programmer to implement it manually. To do that, just enable the Options.Sorting.ManualSort treeview property and subscribe to the ColumnSortOrderChanged treview event where you need to implement your sorting algorithm.

Example:

tree.Options.Sorting.ManualSort = true;
tree.ColumnSortOrderChanged += tree_ColumnSortOrderChanged;
void tree_ColumnSortOrderChanged(FlexibleTreeView pTreeview, ColumnSortOrderChangedEventArgs pArgs)
{
  // custom sorting logic here
}


Separate nodes sorting

While Flexible TreeView supports sorting of all treeview nodes, it also allows you to sort any separate node using the Node.Sort method where you can pass the sort order and custom nodes comparer callback.


Groups always on top

In some cases, you need to keep nodes (so–called ‘groups’) on top of the treeview even after sorting. To do that, enable the Options.Sorting.GroupsAlwaysOnTop treeview property and subscribe to the IsGroupNodes treeview event to define which node is a group and should remain before all other nodes.

Example:

tree.Options.Sorting.GroupsAlwaysOnTop = true;
tree.IsGroupNodes += tree_IsGroupNodes;
 
void tree_IsGroupNodes(FlexibleTreeView pTreeview, GroupNodeEventArgs pArgs)
{
  // move all checked nodes on top.
  if (pArgs.Node1.CheckState == eCheckState.Checked)
    pArgs.IsNode1Group = true;
  if (pArgs.Node2.CheckState == eCheckState.Checked)
    pArgs.IsNode2Group = true;
}


Node post compare audit

If you need to fine-tune the nodes compare result, but you don’t want to completely implement the sorting algorithm, you can use the node post–compare audit mechanism. It allows you to call your code when two nodes have been compared, but haven`t moved yet, so you can change the compare result.
To do that, enable the Options.Sorting.NodePostCompareAudit treeview property and subscribe to the NodePostCompareAudit treeview event where you can change the two nodes compare result.

Example:

tree.Options.Sorting.NodePostCompareAudit = true;
tree.NodePostCompareAudit += tree_NodePostCompareAudit;
void tree_NodePostCompareAudit(FlexibleTreeView pTreeview, NodeCompareEventArgs pArgs)
{
  // move nodes with bigger children nodes count upper
  if (pArgs.Node1.Nodes.Count > pArgs.Node2.Nodes.Count)
  {
    pArgs.CompareResult = (pArgs.SortOrder == SortOrder.Ascending) ? eCompareResult.Higher : eCompareResult.Lower;
  }
}

Previous Top Next


Last news
Bookmark and Share
Use Flexible TreeView in WPF project
Sunday, 18 December 2011

Do you have a WPF project and want to use Flexible TreeView there? No problem!

Flexible TreeView v3.4
Friday, 16 December 2011

Flexible TreeView v3.4 maintenance release has been released.

Flexible TreeView v3.3
Friday, 23 September 2011

Flexible TreeView v3.3 maintenance release has been released. Separate assemblies for .NET 2.0, 4.0 and 4.0 Client Profile, HTML markup extension, etc.

Latest release

Version:
Release date:
3.4
Dec 16, 2011


Copyright © 2006-2012 ARMSoft. All rights reserved.