vb.netdevexpressdevexpress-windows-uixtratreelist

Unable to delete child nodes of a parent node (Devexpress TreeList)


I am trying to delete focused node along with it's all child nodes (if any) from TreeList. The problem is I am unable to retrieve the child nodes. I tried 2 approaches:

  1. I used TreeListNodeIterator which returns a TreeListNodes object containing childs of the focused row:

    Public Class TreeListOperationGetChildNodes
            Inherits TreeListOperation
            Private _child_nodes As TreeListNodes
    
        Public Sub New(tree As TreeList)
            _child_nodes = New TreeListNodes(tree)
        End Sub
    
        Public Overrides Sub Execute(node As DevExpress.XtraTreeList.Nodes.TreeListNode)
            _child_nodes.Add(node)
        End Sub
    
        Public ReadOnly Property ChildNodes() As TreeListNodes
            Get
                Return _child_nodes
            End Get
        End Property
    
    End Class
    

This child_nodes object is then used to delete all nodes from TreeList:

Dim child_nodes As TreeListNodes = New TreeListNodes(DataTreeList)
Dim op As New TreeListOperationGetChildNodes(DataTreeList)
Dim nodes As TreeListNodes = New TreeListNodes(DataTreeList)
DataTreeList.NodesIterator.DoLocalOperation(op, nodes)
child_nodes = op.ChildNodes 

For Each node As TreeListNode In child_nodes
   DataTreeList.DeleteNode(node)
Next
  1. Another Approach I tried was to Loop through all the nodes of the treelist and see if their parent_id is equals to focused node id. If yes then put them into a stack. At the end, for each node in stack call delete node.

    Dim nodeStack As New Stack(Of DevExpress.XtraTreeList.Nodes.TreeListNode)
    nodeStack.Push(DataTreeList.FocusedNode)
    FillStack(DataTreeList.FocusedNode, nodeStack)
    For i As Integer = 0 To nodeStack.Count - 1 Step +1
        DataTreeList.DeleteNode(nodeStack.Pop)
    Next
    
    Private Sub FillStack(ByVal node As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal nodestack As Stack(Of DevExpress.XtraTreeList.Nodes.TreeListNode))
        For Each childnode As DevExpress.XtraTreeList.Nodes.TreeListNode In DataTreeList.Nodes
    
            If DirectCast(DataTreeList.GetDataRecordByNode(childnode), Data).ParentID = _
               DirectCast(DataTreeList.GetDataRecordByNode(node), Data).ID Then
    
                nodestack.Push(childnode)
                FillStack(childnode, nodestack)
    
            End If
        Next
    End Sub
    

The issue with above code snippets:

  1. Here the node iterator is returning chlid_nodes empty (no child nodes found)
  2. In FillDeleteStack DataTreeList.Nodes contains only 2 nodes that is only parent nodes. So stack is always empty.

I can not understand why I am not able to iterate through child nodes. Why the collection of nodes in TreeList contains only parent nodes and not child nodes?


Solution

  • 0. To delete focused node along with it's all child nodes you can simply use TreeList.DeleteNode method:

    DataTreeList.DeleteNode(DataTreeList.FocusedNode)
    

    1. You are using TreeListNodes(TreeList) constructor which creates an empty nodes collection. If you want to get the collection of existing nodes than you can use TreeList.Nodes or TreeListNode.Nodes properties:

    nodes = DataTreeList.Nodes
    ' or
    nodes = DataTreeList.FocusedNode.Nodes
    

    2. TreeList.Nodes property returns the collection of the TreeList's root nodes. If you want to get all visible nodes in TreeList then you can use TreeList.GetNodeList method.