windowsforms-479-controls-and-libraries-vertical-grid-data-layout-records-rows-and-cells-rows-tree-traversal.md
This topic explains how to traverse vertical and property grid’s rows that are organized into a tree.
Follow the steps below to enumerate grid rows and perform a custom operation on each row.
Specify the operation performed on rows.
Enumerate rows and perform the specified operation on each row.
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).
the RowOperation.NeedsVisitChildren method (returns true if not overridden) — returns whether to enumerate the specified row’s child 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.
The following example shows how to count rows:
Implement a count operation.
Invoke the operation.
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; } }
}
Public Class GetRowsAtLevelOperation
Inherits RowOperation
Dim Level As Integer
Dim _Rows As ArrayList
'The row level
'is initialized in the constructor.
Public Sub New(ByVal Level As Integer)
MyBase.New()
Me.Level = Level
_Rows = New ArrayList()
End Sub
'Add a row to the collection,
'if it is at the specified level.
Public Overrides Sub Execute(ByVal Row As BaseRow)
If Row.Level = Level Then
_Rows.Add(Row)
End If
End Sub
'Do not enumerate rows
'at higher levels
'to improve performance.
Public Overrides Function NeedsVisitChildren(ByVal Row As BaseRow) As Boolean
If Row.Level = Level Then Return False
Return True
End Function
Public ReadOnly Property Rows() As ArrayList
Get
Return _Rows
End Get
End Property
End Class
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;
}
Dim Operation As New GetRowsAtLevelOperation(1)
VGridControl1.RowsIterator.DoOperation(Operation)
Dim RowObject As Object
For Each RowObject In Operation.Rows
Dim Row As BaseRow = RowObject
Row.Options = Row.Options And Not VGridOptionsRow.AllowSize
Next
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; } }
}
Public Class CollapseExceptSpecifiedOperation
Inherits RowOperation
Dim VisibleRow As BaseRow
'Specify the row
'in the constructor.
Public Sub New(ByVal VisibleRow As BaseRow)
MyBase.New()
Me.VisibleRow = VisibleRow
End Sub
'Collapse a row if it is not
'the specified row's parent.
Public Overrides Sub Execute(ByVal Row As BaseRow)
If Not Row.HasAsChild(VisibleRow) Then Row.Expanded = False
End Sub
'Do not enumerate rows
'that have no child rows
‘to improve performance.
Public Overrides ReadOnly Property NeedsFullIteration() As Boolean
Get
Return False
End Get
End Property
End Class
The code below collapses all rows except for the focused row’s parents.
vGridControl1.RowsIterator.DoOperation(new CollapseExceptSpecifiedOperation(vGridControl1.FocusedRow));
VGridControl1.RowsIterator.DoOperation(New CollapseExceptSpecifiedOperation(VGridControl1.FocusedRow))