Flexible TreeView 5.1.9 Help
Sorting

Flexible TreeView supports the following sorting modes:


Sorting by columns and node controls bound data

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

Automatic re-sorting will take place when:


Sorting by node controls bound data

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

Automatic re-sorting will take place when:


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, SortContext pContext)
{
  if (pNode1.CheckState == pNode2.CheckState)
    return eCompareResult.Equal;

  // move all checked nodes on top.
  if (pNode1.CheckState == eCheckState.Checked)
    return eCompareResult.Higher;
  if (pNode2.CheckState == eCheckState.Checked)
    return eCompareResult.Lower;

  // use default comparer's result.
  return pContext.DefaultComparer(pNode1, pNode2, pContext);
}
tree.DefaultNodeComparer = MyComparer
tree.ReSort()

' new node comparer where we move all checked nodes on top.
Private Function MyComparer(pNode1 As Node, pNode2 As Node, pContext As SortContext) As eCompareResult
    If pNode1.CheckState = pNode2.CheckState Then
        Return eCompareResult.Equal
    End If
    ' move all checked nodes on top.
    If pNode1.CheckState = eCheckState.Checked Then
        Return eCompareResult.Higher
    End If
    If pNode2.CheckState = eCheckState.Checked Then
        Return eCompareResult.Lower
    End If
    ' use default comparer's result.
    Return pContext.DefaultComparer(pNode1, pNode2, pContext)
End Function


Manual sorting

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 treeview 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
}
tree.Options.Sorting.ManualSort = True
tree.ColumnSortOrderChanged += tree_ColumnSortOrderChanged
Private Sub tree_ColumnSortOrderChanged(pTreeview As FlexibleTreeView, pArgs As ColumnSortOrderChangedEventArgs)
 ' custom sorting logic here
End Sub


Separate nodes sorting

While Flexible TreeView supports sorting of all treeview nodes, it also allows you to sort any particular node by calling the Sort node method where you can pass the desired 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;
}
tree.Options.Sorting.GroupsAlwaysOnTop = True
tree.IsGroupNodes += tree_IsGroupNodes

Private Sub tree_IsGroupNodes(pTreeview As FlexibleTreeView, pArgs As GroupNodeEventArgs)
 ' move all checked nodes on top.
 If pArgs.Node1.CheckState = eCheckState.Checked Then
  pArgs.IsNode1Group = True
 End If
 If pArgs.Node2.CheckState = eCheckState.Checked Then
  pArgs.IsNode2Group = True
 End If
End Sub


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;
  }
}
tree.Options.Sorting.NodePostCompareAudit = True
tree.NodePostCompareAudit += tree_NodePostCompareAudit
Private Sub tree_NodePostCompareAudit(pTreeview As FlexibleTreeView, pArgs As NodeCompareEventArgs)
 ' move nodes with bigger children nodes count upper
 If pArgs.Node1.Nodes.Count > pArgs.Node2.Nodes.Count Then
  pArgs.CompareResult = If((pArgs.SortOrder = SortOrder.Ascending), eCompareResult.Higher, eCompareResult.Lower)
 End If
End Sub

 

 


Copyright © 2006-2016 ARMSoft

www.FlexibleTreeView.com