I am trying to copy the whole tree (exactly all the nodes) of the tree (completely) to another tree using this code:
TreeNodeCollection myTreeNodeCollection = treeView1.Nodes; TreeNode[] myTreeNodeArray = new TreeNode[treeView1.Nodes.Count]; treeView1.Nodes.CopyTo(myTreeNodeArray, 0); treeView2.Nodes.AddRange(myTreeNodeArray);
But this does not allow me to do this, he asks either to remove the nodes in the source tree, or use it Clone! How can i do this? I do not want my source tree to lose anything in this process.
** UPDATE ** Ok guys, I found complex code (for me !!), but how can I use this?
public static T DeepTreeCopy<T>(T obj) { object result = null; using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0; result = (T)formatter.Deserialize(ms); ms.Close(); } return (T)result; }
c # clone copy treeview
Saeid yazdani
source share