// Manage Tree View Nodes in RunTime
// In order to manage a node you should set a key for each node
// in your case records id's could be fine
treeView1.Nodes.Add("RootNode1Key","Root Node1");
treeView1.Nodes.Add("RootNode2Key", "Root Node2");
// adds a node in runtime with title Root Node
//Now add a Child to "Root Node 2"
// first find the node and make a pointer to it :^)
TreeNode FoundedNode;
FoundedNode = treeView1.Nodes.Find("RootNode2Key", true)[0];
// if you create a unique key for each node the find function returns
// a tree node array with only one node and you access it like above
// else you retrieve many records of TreeNode
FoundedNode.Nodes.Add("RootNode2Childe1", "Root Node2 Child 1");
// hey now you added a child node to RootNode2
// you want to add a child for it in depth
// 2 ways :
// 1: yous find method treeView1.Nodes.Find("RootNode2Childe1", true)[0];
// 2: Following
FoundedNode.Nodes["RootNode2Childe1"].Nodes.Add("RootNode2Childe1Child1", "my parent is RootNode2Childe1");
现在,如果您想删除一个,很容易,如下所示
private void button2_Click(object sender, EventArgs e)
{
// i am sure you know how to add nodes now remove a node
TreeNode FoundedNode;
FoundedNode = treeView1.Nodes.Find("RootNode2Childe1Child1", true)[0];
FoundedNode.Remove();
}
但我建议您在网上搜索有一些方法和更好的解决方案,您可以使用数据源,然后当一条记录添加到您的表中并删除一条记录时,您的树视图会更新,反之亦然
*向其他伊朗人和程序员提供该社区的会员资格 *