How to add nodes of one tree instance to another instance of one tree

How to populate nodes in newtreeview1, which is an instance of another treeview1? Nodes that are added to "newtreeview1" should be available in the first instance of treeview1?

eg; if treeview1 contains

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

newtreeview1 must also have the above nodes.

+3
source share
4 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);
+1
source

You need to copy the nodes. Sort of:

otherTreeView.Nodes.Add(node.Text);

, , , , (, / ). , .

0

You can try the approach indicated in the link below and serialize the contents of the tree. Then create a new tree based on serialized content. I know a lot of long approach, but it is guaranteed to add all hierarchical data to the second tree view.

Save knots from wood

0
source

You can simply copy an instance of TreeView1 and add additional nodes. Same as below

TreeView2 = TreeView1;
TreeView2.Nodes.Add(new TreeNode());
0
source

All Articles