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:
I used
TreeListNodeIteratorwhich returns aTreeListNodesobject 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
Another Approach I tried was to Loop through all the nodes of the treelist and see if their
parent_idis equals to focused nodeid. 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:
- Here the node iterator is returning
chlid_nodesempty (no child nodes found) - In
FillDeleteStackDataTreeList.Nodescontains 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?
0. To delete focused node along with it's all child nodes you can simply use
TreeList.DeleteNodemethod: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 useTreeList.NodesorTreeListNode.Nodesproperties:2.
TreeList.Nodesproperty returns the collection of theTreeList's root nodes. If you want to get all visible nodes inTreeListthen you can useTreeList.GetNodeListmethod.