Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add nodes of one instance of treeview to the another instance of same treeview

How to populate the nodes into the newtreeview1 which is the instance of the another treeview1 ? The nodes which is added to the "newtreeview1" should be available in the first instance of the treeview1?

for example; if the treeview1 contains

   |-- Node1
        |-- Node2
           | -- Node3
        |-- Node4

the newtreeview1 should also have the above nodes.

like image 449
Tanya Avatar asked Dec 15 '25 14:12

Tanya


1 Answers

you can do this by cloning each node like this

    private void CopyNodes(TreeView srcTree, TreeView dstTree)
    {
        var ar = System.Array.CreateInstance(typeof(TreeNode), srcTree.Nodes.Count);
        treeView1.Nodes.CopyTo(ar, 0);
        foreach (TreeNode item in ar)
        {
            dstTree.Nodes.Add((TreeNode)item.Clone());
        }
    }

and call the function

CopyNodes(treeView1, treeView2);
like image 182
DeveloperX Avatar answered Dec 17 '25 06:12

DeveloperX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!