Populating tree nodes of a Tree view control in C#.NET


//How can I populate tree nodes of a Tree View Control in C#.NET?

//Code by Kiran Reddy
//TreeViewNodes
//First add one parent node to the Tree View control in its properties, select the parent node, to add child nodes to the Tree view add following code to button click events

TreeNode tvnode = new TreeNode("your_Node_Name");
treeView1.SelectedNode.Nodes.Add(tvnode);
treeView1.ExpandAll();
if(treeView1.SelectedNode.Nodes.Count > 1 && treeView1.SelectedNode.ForeColor != Color.Blue)
treeView1.SelectedNode.ForeColor = Color.Green;

//Add siblings to treeview

TreeNode tvnode = new TreeNode("your_Node_Name");
tvnode.ForeColor = Color.Brown;
treeView1.SelectedNode.Parent.Nodes.Add(tvnode);

//Delete selected node
treeView1.SelectedNode.Remove();


You can view this article online at:
http://www.kiranreddys.com/knowledgebase/index.php/article/populating-tree-nodes-of-a-tree-view-control-in-cn