Back to Devexpress

Iterate Through Nodes

wpf-9570-controls-and-libraries-data-grid-grid-view-data-layout-nodes-iterate-through-nodes.md

latest3.2 KB
Original Source

Iterate Through Nodes

  • Apr 04, 2019
  • 2 minutes to read

The Node Iterator allows you to traverse through the nodes without writing recursive code. Nodes are visited one by one starting from a specified node down to the last node contained within a tree.

The image below illustrates the sequence in which nodes are processed.

To iterate through the nodes, do the following.

Example: How to Iterate Through All Visible Nodes

This example shows how to traverse through all visible nodes to expand ones that have 4 or more child nodes. Nodes that have less than 4 child nodes are collapsed.

View Example: Iterate Through Nodes With the TreeListNodeIterator

xaml
<dxg:GridControl x:Name="grid" 
                 AutoGenerateColumns="AddNew" 
                 EnableSmartColumnsGeneration="True" 
                 Loaded="OnGridLoaded">
    <dxg:GridControl.View>
        <dxg:TreeListView x:Name="view" AutoWidth="True"
                          KeyFieldName="ID" ParentFieldName="ParentID"/>
    </dxg:GridControl.View>
</dxg:GridControl>
cs
void SmartExpandNodes(int minChildCount) {
    TreeListNodeIterator nodeIterator = new TreeListNodeIterator(view.Nodes, true);
    while (nodeIterator.MoveNext())
        nodeIterator.Current.IsExpanded = nodeIterator.Current.Nodes.Count >= minChildCount;
}

void OnGridLoaded(object sender, RoutedEventArgs e) {
    SmartExpandNodes(4);
}
vb
Private Sub SmartExpandNodes(ByVal minChildCount As Integer)
    Dim nodeIterator As TreeListNodeIterator = New TreeListNodeIterator(Me.view.Nodes, True)
    While nodeIterator.MoveNext()
        nodeIterator.Current.IsExpanded = nodeIterator.Current.Nodes.Count >= minChildCount
    End While
End Sub

Private Sub OnGridLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    SmartExpandNodes(4)
End Sub