Copy all treeView parent and children to another treeView c# WinForms

前端 未结 4 1299
梦谈多话
梦谈多话 2020-12-17 21:32

I am trying to copy the entire tree (exactly all nodes) of a treeview (completely) to another treeview using this code:

        TreeNodeCollection myTreeNode         


        
4条回答
  •  無奈伤痛
    2020-12-17 22:18

    Using the node.Clone() function works as well...

    foreach (TreeNode node in treeViewSource.Nodes)
    {
        treeViewTarget.Nodes.Add((TreeNode)node.Clone());
    }
    

    Adding a root node will help ensure the "PlusMinus" functionality is viewable.

    TreeNode rootNode = new TreeNode("Root Node");
    treeViewTarget.Nodes.Add(rootNode);
    foreach (TreeNode node in treeViewSource.Nodes)
    {
        rootNode.Nodes.Add((TreeNode)node.Clone());
    }
    rootNode.Expand();
    

提交回复
热议问题