By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,057 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,057 IT Pros & Developers. It's quick & easy.

Copy nodes of one treeview to another

P: n/a
Hi
How to copy treeview.Nodes from one treeview to another.
Is this posible to do this in one line ? ( Calling some function )
Sep 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I have found the answer ( if someone will need in the future ):

TreeNodeCollection myTreeNodeCollection = myTreeViewBase.Nodes;
// Create an array of 'TreeNodes'.
TreeNode[] myTreeNodeArray = new TreeNode[myTreeViewBase.Nodes.Count];
// Copy the tree nodes to the 'myTreeNodeArray' array.
myTreeViewBase.Nodes.CopyTo(myTreeNodeArray,0);
// Remove all the tree nodes from the 'myTreeViewBase' TreeView.
myTreeViewBase.Nodes.Clear();
// Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
myTreeViewCustom.Nodes.AddRange(myTreeNodeArray);
Sep 21 '06 #2

P: n/a
A function like this should do it.

void CopyNodes(TreeView from, TreeView to)
{
foreach (TreeNode node in from)
{
to.Nodes.Add(node.Clone());
}
}

HTH

Ciaran O'Donnell
"PiotrKolodziej" wrote:
Hi
How to copy treeview.Nodes from one treeview to another.
Is this posible to do this in one line ? ( Calling some function )
Sep 21 '06 #3

P: n/a
That moves them, rather than copies them like you originally requested. If
that is what you want to do then use this as CopyTo uses Array.Copy which is
faster then just a loop.

Ciaran O'Donnell
"PiotrKolodziej" wrote:
I have found the answer ( if someone will need in the future ):

TreeNodeCollection myTreeNodeCollection = myTreeViewBase.Nodes;
// Create an array of 'TreeNodes'.
TreeNode[] myTreeNodeArray = new TreeNode[myTreeViewBase.Nodes.Count];
// Copy the tree nodes to the 'myTreeNodeArray' array.
myTreeViewBase.Nodes.CopyTo(myTreeNodeArray,0);
// Remove all the tree nodes from the 'myTreeViewBase' TreeView.
myTreeViewBase.Nodes.Clear();
// Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
myTreeViewCustom.Nodes.AddRange(myTreeNodeArray);
Sep 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.