Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Iterate through nodes and calculate the number of nodes at a specific level

  • 2 minutes to read

The following example shows how to use the Nodes Iterator to obtain the number of nodes which reside on the specified nesting level.

In the example, a CustomNodeOperation object is created that is used to calculate this number. The operation class contains an internal counter that is incremented by one each time a node that resides at the specified nesting level is accessed.

To get the total number of the nodes which reside at the specified nesting level, a CustomNodeOperation instance is created and passed to the TreeListNodesIterator.DoLocalOperation method. After the method has been performed, the CustomNodeOperation.NodeCount property is read to get the number of nodes.

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

// Declaring the custom operation class.
class CustomNodeOperation : TreeListOperation {
   int level;
   int nodeCount;
   public CustomNodeOperation(int level) : base() {
      this.level = level;
      this.nodeCount = 0;
   }
   public override void Execute(TreeListNode node) {
      if(node.Level == level)
         nodeCount++;
   }
   public int NodeCount {
      get { return nodeCount; }
   }
}

// ...

CustomNodeOperation operation = new CustomNodeOperation(2);
treeList1.NodesIterator.DoLocalOperation(operation, treeList1.Nodes);
int totalNodesAtSecondLevel = operation.NodeCount;