How to: Iterate through nodes and collapse specific nodes

  • 2 minutes to read

The following example shows how to create an operation class that will collapse all the nodes which do not contain the specified node as their child. Only the node's parents will be expanded as a result.

The operation class accepts the node as its constructor parameter and stores it in an internal variable. The Execute method checks whether the processed node contains the specified node as a child. If not, the processed node is collapsed. The operation class also overrides the TreeListOperation.NeedsFullIteration property to process only the nodes that have children. This provides performance benefits when working with large and complex node structures.

The image below shows the Tree List before and after executing the sample code:

CD_UsingNodesIterator

using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Nodes.Operations;

public class CollapseExceptSpecifiedOperation : TreeListOperation {
   TreeListNode visibleNode;
   public CollapseExceptSpecifiedOperation(TreeListNode visibleNode) : base() {
      this.visibleNode = visibleNode;
   }
   public override void Execute(TreeListNode node) {
      if (!visibleNode.HasAsParent(node))
         node.Expanded = false;
   }
   public override bool NeedsFullIteration { get { return false; } }
}

//...

treeList1.NodesIterator.DoOperation(new CollapseExceptSpecifiedOperation(treeList1.FocusedNode));