Adding childs to a treenode dynamically in c#

后端 未结 2 464
面向向阳花
面向向阳花 2021-01-14 02:07

I want to dynamically add some child nodes to a root node in my TreeView. I have a string Array of some names like {\"john\", \"sean\", \"edw

2条回答
  •  半阙折子戏
    2021-01-14 02:49

    You need to append the child node to the parent, here's an example:

    TreeView myTreeView = new TreeView();
    myTreeView.Nodes.Clear();
    foreach (string parentText in xml.parent)
    {
       TreeNode parent = new TreeNode();
       parent.Text = parentText;
       myTreeView.Nodes.Add(treeNodeDivisions);
    
       foreach (string childText in xml.child)
       {
          TreeNode child = new TreeNode();
          child.Text = childText;
          parent.Nodes.Add(child);
       }
    }
    

提交回复
热议问题