1

Here's my code:

public Form1()
{
    InitializeComponent();
    treeView1.Nodes.Add(new TreeNode("Graphic Requests"));
    if (TreeNodesList == null) TreeNodesList = new List<TreeNode>();
    TreeNodesList.Add(new TreeNode("Art Not Started"));
    TreeNodesList.Add(new TreeNode("Art In Progress"));
    TreeNodesList.Add(new TreeNode("Items To Accept/Modify"));
    TreeNodesList.Add(new TreeNode("Final Art Not Locked"));

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

I was expecting to have a parent node, "Graphic Requests", with the remaining nodes added as children. However, my list looks like this:

Resulting list

Can someone tell me what I'm missing?

Kevin
  • 4,798
  • 19
  • 73
  • 120

5 Answers5

2

You're adding the nodes to treeView1 that's why they are being parent nodes instead of children. Rather add them to the first node that you added.

Try this:

public Form1()
{
   InitializeComponent();
   var parent = new TreeNode("Graphic Requests");

   TreeNodesList.Add(new TreeNode("Art Not Started"));
   TreeNodesList.Add(new TreeNode("Art In Progress"));
   TreeNodesList.Add(new TreeNode("Items To Accept/Modify"));
   TreeNodesList.Add(new TreeNode("Final Art Not Locked"));

   foreach (var node in TreeNodesList)
   {
       parent.Nodes.Add(node);
   }
   treeView1.Nodes.Add(parent);
}
Dumisani
  • 2,988
  • 1
  • 29
  • 40
  • Ack! I was adding the nodes to the treeview itself, not the parent node in the treeview. Thanks so much! I'll mark the answer as soon as the timer is up. – Kevin Mar 12 '14 at 14:55
  • 1
    You're welcome. It's the simple things that we miss ;) – Dumisani Mar 12 '14 at 14:59
1

You need to add the sub-nodes to the parent-node's Node-collection, or else they are not child nodes, but rather siblings to the parent node.

public Form1()
{
    InitializeComponent();

    var parentNode = new TreeNode("Graphic Requests");

    if (TreeNodesList == null) TreeNodesList = new List<TreeNode>();
    TreeNodesList.Add(new TreeNode("Art Not Started"));
    TreeNodesList.Add(new TreeNode("Art In Progress"));
    TreeNodesList.Add(new TreeNode("Items To Accept/Modify"));
    TreeNodesList.Add(new TreeNode("Final Art Not Locked"));

    foreach (var node in TreeNodesList)
    {
        parentNode.Nodes.Add(node);
    }
    treeView1.Nodes.Add(parentNode);
}
Johan Tegman
  • 102
  • 4
1

You first need to add the root node to the tree and only later to add the child nodes:

treeView1.Nodes.Add(new TreeNode("Graphic Requests")); // ROOT NODE

TreeNode parentNode = treeView2.Nodes[0];
if (parentNode != null)
{
  parentNode.Add(new TreeNode("Art Not Started"));
  parentNode.Add(new TreeNode("Art In Progress"));
  parentNode.Add(new TreeNode("Items To Accept/Modify"));
  parentNode.Add(new TreeNode("Final Art Not Locked"));
}
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
1

You're in fact adding all your nodes to treeview directly. Should add your child nodes to "Graphic" that is your parent node, and then your parent node to your treeview. Try this:

public Form1()
{
    InitializeComponent();
    TreeNode parent = new TreeNode("Graphic Requests")
    if (TreeNodesList == null) TreeNodesList = new List<TreeNode>();
    TreeNodesList.Add(new TreeNode("Art Not Started"));
    TreeNodesList.Add(new TreeNode("Art In Progress"));
    TreeNodesList.Add(new TreeNode("Items To Accept/Modify"));
    TreeNodesList.Add(new TreeNode("Final Art Not Locked"));

    foreach (var node in TreeNodesList)
    {
        parent.Nodes.Add(node);
    }
    treeView1.Nodes.Add(parent);
}
VirgileD
  • 737
  • 1
  • 7
  • 23
0

You have to do something like this to append to the parent :

foreach (var node in TreeNodesList)
{
    treeView1.Nodes[0].Nodes.Add(node);
}

If you want to find more information about filling the TreeView I recommend you the following article

Community
  • 1
  • 1
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105