I have a parent node which has a child, and this child has another child etc.. and they are all in a TreeView
So I create a global variable to save all my nodes as:
private TreeNodeCollection ProjectTreeView { get; set; }
Then I set data of the tree node into my global variable:
ProjectTreeView = this.tvProjectList.Nodes[0].Nodes;
And When I click a button I want to filter my TreeView, so first I clear the TreeView, then I iterate over the collection and only show the nodes that meet my condition:
private void rdoIssued_Click(object sender, EventArgs e)
{
//blocks repainting tree till all objects loaded
this.tvProjectList.BeginUpdate();
this.tvProjectList.Nodes.Clear();
foreach (TreeNode projectNode in ProjectTreeView)
{
if (bool.Parse(projectNode.Tag.ToString().Split('|')[8]) == true)
{
this.tvProjectList.Nodes.Add((TreeNode)projectNode.Clone());
}
}
//enables redrawing tree after all objects have been added
this.tvProjectList.EndUpdate();
}
The problem is it only clone the first Node but not the children. How can I clone a node with all children?
Clone
method only creates a shallow copy. Have you tried not creating a clone? All the children are already attached toprojectNode
and should follow their parent if you add it to the tree. – MikeH