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.
Enumerate rows and perform the specified operation on each row.
- Use the VGridControlBase.RowsIterator property to get an iterator.
- Pass the created operation to the VGridRowsIterator.DoOperation or VGridRowsIterator.DoLocalOperation method.
Enumerate Specific Rows
You can also override the operation’s following properties and methods:
the RowOperation.NeedsFullIteration property (true if not overridden) — gets or sets whether to enumerate all rows or only rows that have child rows (see BaseRow.HasChildren).
Tip
Use the RowOperation.NeedsFullIteration property to improve performance. For instance, if the operation expands parent rows, there is no need to enumerate rows that have no child rows.
the RowOperation.NeedsVisitChildren method (returns true if not overridden) — returns whether to enumerate the specified row’s child rows.
Tip
Use the RowOperation.NeedsVisitChildren method to improve performance. For instance, if you need to only iterate root rows.
- the RowOperation.CanContinueIteration method (returns true if not overridden) — called before and after the operation is executed on the current row (passed as the row parameter) and returns whether to continue to enumerate rows.
- the RowOperation.Init method — called before an enumeration.
- the RowOperation.Release method — called after an enumeration.
How to: Count Rows
The following example shows how to count rows:
Implement a count operation.
Invoke the operation.
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.