Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello experts,

I have a window forms application where there are already treeview designed.
But I want to add a Node and child to that nodes based on condition.
That Node should be on the TOP of all the existing nodes designed in designer.

This is what I have tried but the nodes are being added to the frst node already in designer .

For example My existing Nodes are like

C#
Node1
 Node1 child 1 
 Node 1 Child 2

Node2 
 Node 2 Child 1 
continues 


Now My code does below
C#
Node1
 Node1 child1 
 Node1 child2 
 Top Node
 A
 B
 C
Node 2
 Node 2 child1




However I am looking for something like below

C#
Top Node
 A
 B
 C
Node 1 
 Node 1 Child 1
 Node 1 Child 2 
Node 2 
 Node 2 child 1 
continues 




Could someone please help me

What I have tried:

If TreeView1.Nodes.Count > 0 Then
TreeView1.Nodes(0).Nodes.Insert(0, "TopNode")

TreeView1.Nodes(0).Nodes.Add("A")
TreeView1.Nodes(0).Nodes.Add("B")
TreeView1.Nodes(0).Nodes.Add("C")
End if
Posted
Updated 31-Mar-16 6:01am

1 solution

You don't Insert your TopNode in the Nodes collection of one of the other nodes.

You have to iterate through the TreeView Nodes collection and add those Nodes to the Node collection of your TopNode. You then have to remove those nodes from the TreeView Nodes collection and add back your TopNode to the TreeView Nodes collection. Your TopNode will be the only node in the TreeView Nodes collection. Everything else will hang off of your TopNode.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900