2. add and delete Tree nodes
To add, delete, or modify a node in a Treeview, you only need to use the add, remove, and other methods of the nodes attribute. It is worth noting that. in net, the nodes set of Treeview is different from that in vs6.0. vs6.0 is a large set, while. in. net, each node in the hierarchy has the nodes attribute. The addition, deletion, and modification of Tree nodes are significantly different from those of vs6.0, especially when a tree node is deleted.
Private sub butadd_click (byval sender as system. Object, byval e as system. eventargs) handles butadd. click' Add a subnode under the selected node Dim tmpnd as new treenode (), ndsel as treenode Tmpnd. ID = getnewid () Ndsel = treeview1.getnodefromindex (treeview1.selectednodeindex) 'selected node Tmpnd. Text = "new node" Ndsel. nodes. Add (tmpnd) Dim myrow as datarow Myrow = Ds. Tables ("Tree"). newrow () Myrow ("node_name") = tmpnd. ID Myrow ("node_descript") = "new node" & tmpnd. ID & "_" & ndsel. ID Myrow ("parent_name") = ndsel. ID DS. Tables ("Tree"). Rows. Add (myrow) End sub Private sub butdele_click (byval sender as object, byval e as system. eventargs) handles butdele. click' Delete the selected node Dim idx as string = treeview1.selectednodeindex () Getndcol (idx). Remove (treeview1.getnodefromindex (idx )) Dim DV as new dataview (), recno as integer DV. Table = Ds. Tables ("Tree ") DV. rowfilter = "nodeid =" & ndid DV. Delete (0) End sub Private function getndcol (byval idx as string) as treenodecollection 'Obtain the nodes set of the parent node of the selected node. Dim CNT as integer, I as integer Dim tmpnds as treenodecollection Dim idxs () as string Idxs = Split (idx ,".") CNT = ubound (idxs) If CNT = 0 then Tmpnds = treeview1.nodes Else Tmpnds = treeview1.nodes (CINT (idxs (0). Nodes For I = 1 to CNT-1 Tmpnds = tmpnds (CINT (idxs (I). Nodes Next End if Return tmpnds End Function |