Skip to main content
A newer version of this page is available. .

Tree Traversal

  • 5 minutes to read

This topic explains how to traverse vertical and property grid’s rows that are organized into a tree.

Enumerate All Rows

Follow the steps below to enumerate grid rows and perform a custom operation on each row.

  • Specify the operation performed on rows.

    • Inherit from the RowOperation abstract class. This class specifies an operation performed on rows.
    • Override the RowOperation.Execute method. This method is called each time a row is enumerated and accepts the row as a parameter.
    public class MyOperation : RowOperation {
        public override void Execute(BaseRow row) {
            //Do something.
        }
    }
    
  • Enumerate rows and perform the specified operation on each row.

    vGridControl1.RowsIterator.DoOperation(new MyOperation());
    

Enumerate Specific Rows

You can also override the operation’s following properties and methods:

How to: Count Rows

The following example shows how to count rows:

  • Implement a count operation.

    public class CountOperation : RowOperation {
       int totalRows;
       public CountOperation() : base() {
          totalRows = 0;
       }
       public override void Execute(BaseRow row) {
          totalRows++;
       }
       public int TotalRows { get { return totalRows; } }
    }
    
  • Invoke the operation.

    CountOperation operation = new CountOperation();
    vGridControl1.RowsIterator.DoOperation(operation);
    int totalRows = operation.TotalRows;
    

How to: Obtain Rows at Specific Level

This following example shows how to obtain a collection of rows at the specified level.

public class GetRowsAtLevelOperation : RowOperation {
   int level;
   ArrayList rows;
//The row level
//is initialized in the constructor.
   public GetRowsAtLevelOperation(int level) : base() {
      this.level = level;
      rows = new ArrayList();
   }
//Add a row to the collection
//if it is at the specified level.
   public override void Execute(BaseRow row) {
      if (row.Level == level)
         rows.Add(row);
   }
//Do not enumerate rows
//at higher levels
//to improve performance.
   public override bool NeedsVisitChildren(BaseRow row) {
      if (row.Level == level) 
         return false;
      return true;
   }
   public ArrayList Rows { get { return rows; } }
}

The code below prohibits users to resize rows at the first level.

GetRowsAtLevelOperation operation = new GetRowsAtLevelOperation(1);
vGridControl1.RowsIterator.DoOperation(operation);
foreach (object rowObject in operation.Rows) {
   BaseRow row = rowObject as BaseRow;
   row.Options &= ~VGridOptionsRow.AllowSize;
}

How to: Collapse Specific Rows

The following example shows how to collapse all rows except for the specified row’s parents.

public class CollapseExceptSpecifiedOperation : RowOperation {
   BaseRow visibleRow;
//Specify the row
//in the constructor.
   public CollapseExceptSpecifiedOperation(BaseRow visibleRow) : base() {
      this.visibleRow = visibleRow;
   }
//Collapse a row if it is not
//the specified row's parent.
   public override void Execute(BaseRow row) {
      if (!row.HasAsChild(visibleRow))
         row.Expanded = false;
   }
//Do not enumerate rows
//that have no child rows
//to improve performance.
   public override bool NeedsFullIteration { get { return false; } }
}

The code below collapses all rows except for the focused row’s parents.

vGridControl1.RowsIterator.DoOperation(new CollapseExceptSpecifiedOperation(vGridControl1.FocusedRow));