Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why TreeView control creating blank node?

Frist see following code and images.

Code A

treeView1.Nodes.Add("Node A");
treeView1.Nodes.Add("Node B");

Output :
enter image description here


Code B

        TreeNode tn = new TreeNode();
        tn.Nodes.Add("Node A");
        tn.Nodes.Add("Node B");
        treeView1.Nodes.Add(tn);

enter image description here


Now my problem is that treeView1.Nodes.Add(tn); creating a blank node, but my requirement is like Code A's image type (without blank node). If you need any other information please let me know.
UPDATE
Actually Ithere is a function in my code which returns a TreeNode and I have to add this node to TreeView control without first blank level.

like image 446
jams Avatar asked Apr 27 '26 19:04

jams


1 Answers

This code:

TreeNode tn = new TreeNode();

creates an actual item. You didn't give it any text, so it appears blank. Then next two lines are adding child nodes to the blank node.

If your goal is the code in "A", why are you writing "B"?

Edit: response to your updated question

You have a function returning a root blank tree node, which contains children you want. So, something like this is in order.

foreach (var node in returnedNode.Nodes)
{
    treeView1.Nodes.Add(node);
}

OR

treeView1.Nodes.AddRange(returnedNode.Nodes.Cast<TreeNode>().ToArray());
like image 154
John Fisher Avatar answered Apr 29 '26 08:04

John Fisher