Reputation: 1024
I have below tree view and want to select child of child nodes:
I want to select node Group B2. How can I do this ?
Upvotes: 0
Views: 980
Reputation: 23833
You want to do something like
TreeNode nodeSectionB = this.TreeView.Nodes["Section B"]; // Or .Nodes[1];
TreeNode node = nodeSectionB.Nodes["Group B2"]; // Or .Nodes[1];
this.TreeView.SelectedNode = node;
Note, that when using strings to reference nodes, it is assumed that you have provided a reference to do so. Otherwise you will have to use the node indexes. The above assumes that the tree is static and the poistions of the node your after does not change.
If the tree is dynamic, that is the position of the required node does change, you may have to loop though the tree to find the required node. To do this use something like
private void SelectTreeNode(TreeView treeView, string nodeText)
{
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
CheckRecursive(n, nodeText);
}
private void CheckRecursive(TreeNode treeNode, string nodeText)
{
foreach (TreeNode tn in treeNode.Nodes)
if (String.Compare(tn.Text, nodeText, true) == 0)
this.TreeView.SelectedNode = tn;
}
where it is assumed the this.TreeView
is an accessor for your tree view. nodeText
here is the required node text to find - this may have to be ammended for nodes with the same textual values.
I hope this helps.
Upvotes: 2