简体   繁体   中英

Error in Copy one node with its children in Treeview to another treeview

I am creating a new treeview and then want to copy some of other nodes to it. But I have the following error:

Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.

My code is as:

first_treeview.Nodes.Clear();
//treeView2.Nodes.Clear();
int length_last = treeview1.Nodes[0].Nodes.Count;
for (int ii = 0; ii < length_last - 1; ii++)
{
    TreeNode temp_node = treeView1.Nodes[0].Nodes[ii];
    first_treeview.Nodes.Add(temp_node);
}

and the XML file:

<?xml version="1.0" encoding="utf-8"?>
<component version="11">
<config />
<protect>
    <this>
        <user ="Mike"/>
    </this>
</protect>  
<actions>
    <action name="test">
        <obj name="system">             
        </obj>
    </action>
</actions>
</component>

The error message says that one node cannot belong to 2 treeviews. So you have to clone it before adding it to first_treeview .

first_treeview.Nodes.Add((TreeNode) temp_node.Clone());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM