Adding childs to a treenode dynamically in c#

后端 未结 2 438
面向向阳花
面向向阳花 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:42

    I think the problem is that you do not inform the mainNode that child is its children, something like: mainNode.Children.Add(child) (in the for block). You just create the child node, but you don't do anything with it for it to have any relation with the TreeView or with the mainNode.

    0 讨论(0)
  • 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);
       }
    }
    
    0 讨论(0)
提交回复
热议问题