Back to Devexpress

Tree Traversal

windowsforms-479-controls-and-libraries-vertical-grid-data-layout-records-rows-and-cells-rows-tree-traversal.md

latest6.1 KB
Original Source

Tree Traversal

  • Jul 29, 2019
  • 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.

  • Enumerate rows and perform the specified operation on each row.

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.

  • 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.

csharp
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; } }
}
vb
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.

csharp
GetRowsAtLevelOperation operation = new GetRowsAtLevelOperation(1);
vGridControl1.RowsIterator.DoOperation(operation);
foreach (object rowObject in operation.Rows) {
   BaseRow row = rowObject as BaseRow;
   row.Options &= ~VGridOptionsRow.AllowSize;
}
vb
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

How to: Collapse Specific Rows

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

csharp
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; } }
}
vb
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 rowsto 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.

csharp
vGridControl1.RowsIterator.DoOperation(new CollapseExceptSpecifiedOperation(vGridControl1.FocusedRow));
vb
VGridControl1.RowsIterator.DoOperation(New CollapseExceptSpecifiedOperation(VGridControl1.FocusedRow))