Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add nodes to a treeview programatically?

Tags:

c#

winforms

How to add nodes dynamically to a already existing treeview?

if an example as,

-Root
  -child1

above one is already existing treeview. but i want to add one more node(child2) to the Root, output is like..

-Root
  -child1
  -child2
like image 702
kik Avatar asked Feb 23 '23 22:02

kik


2 Answers

Try this:

TreeNode rootNode = TreeView.Nodes.Cast<TreeNode>().ToList().Find(n => n.Text.Equals("Root"));
if (rootNode != null)
{
    rootNode.Nodes.Add("child2");
}
like image 142
Alex Mendez Avatar answered Feb 26 '23 13:02

Alex Mendez


try:

treeView1.Nodes.Add(new TreeNode())

Details are found here: http://msdn.microsoft.com/de-de/library/system.windows.forms.treeview.nodes.aspx

like image 41
user492238 Avatar answered Feb 26 '23 13:02

user492238