0

So I have a TreeView setup with a few nodes in it. I have a string list with a few strings in it, and I want to add the entire list into a child of a specific node in the TreeView using C# code (possibly a foreach loop?). How might I do that?

2 Answers 2

2

This will add three nodes to the child node "node00" of node "node0"

List<string> strings = new List<string>() { "string1", "string2", "string3" };

foreach (string s in strings)
    treeView1.Nodes["node0"].Nodes["node00"].Nodes.Add(s, s);

enter image description here

Note that you can specify the nodes by name (key) or by index

Sign up to request clarification or add additional context in comments.

Comments

1

Are you referring to the WPF TreeView or WinForms TreeView?

For WPF, the approach that worked best for me so far is to create a viewmodel and bind it to TreeView using HierarchicalDataTemplate. Basic idea is explained at http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx.

WinForms TreeView can be manipulated directly through TreeView.Nodes, TreeNode.Nodes and such. I recommend against trying to do something similar with WPF TreeView...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.