How can I add a node to the selected node in a TreeView?
I create a tre开发者_如何学Pythoneview with several nodes in a WinForms application. I want to add a node to one of this nodes (to a child node).
How do I can do this? Thanks.
Every Node in treeview(winforms) is having a collection of Nodes so if you got to the node reference than just adding a new node under its collection will do the job
TreeNode node = treeView.SelectedNode;
node.Nodes.Add(new TreeNode());
// Add a root TreeNode for each Customer object in the ArrayList.
foreach(Customer customer2 in customerArray)
{
treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
// Add a child treenode for each Order object in the current Customer object.
foreach(Order order1 in customer2.CustomerOrders)
{
treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
new TreeNode(customer2.CustomerName + "." + order1.OrderID));
}
}
Entire sample on MSDN...
By simply using the Add
method of the Node
collection of a TreeNode
ie.
treeNode.Nodes.Add(new TreeNode("Node Name"));
精彩评论