Search code examples
c#dynamictreeviewtreenode

adding node dynamically to treeview without using "SelectedNode"


I have a tree view that contains 3 roots and each root contains 3 children. I am wanting to add a node to one of the children. How do i do this using the node "name" instead of the user selecting a node (without the SelectedNode method).

For example:

  • root1
    • a
    • b
    • c
  • root2
    • d
    • e
    • f
  • root3
    • g
    • h
    • i

The user will basically select which child to insert the node at by specifying the name of the node in a text box. After the user specifies inserting "foobar" into the node "g", the tree view would look like the following:

  • root1
    • a
    • b
    • c
  • root2
    • d
    • e
    • f
  • root3
    • g
      • foobar
    • h
    • i

Thanks!


Solution

  • I figured it out.

    TreeView.Nodes["root3"].Nodes["g"].Nodes.Add(node);