It depends on what basis you have to find the node, if you know the ValuePath of the node then you can use FindNode like this -
TreeNode
node= TreeView1.FindNode("Root/Leaf");
node.Selected = true;
else if you want to select a node on the basis of node text, then you will have to iterate through the Treeviews NodesCollection and match on the basis of text and then select the matching node. If you need to do like this then i can provide the function
i used.
else if you know the index of your node then you can do like this
TreeView1.Nodes[4].Selected =
true;
Similar to what Mikael said(but I think that will not work as TreeView.SelectedNode is only a Get property)
Hope it helps.....
When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit for being helpful (and makes search more relevant too).
akjoshi
Contributor
3636 Points
615 Posts
Re: Treeview - Select Node Programatically
Jan 23, 2007 02:26 PM|LINK
Hi peter,
It depends on what basis you have to find the node, if you know the ValuePath of the node then you can use FindNode like this -
TreeNode
node= TreeView1.FindNode("Root/Leaf");node.Selected = true;
else if you want to select a node on the basis of node text, then you will have to iterate through the Treeviews NodesCollection and match on the basis of text and then select the matching node. If you need to do like this then i can provide the function i used.
else if you know the index of your node then you can do like this
TreeView1.Nodes[4].Selected =
true;Similar to what Mikael said(but I think that will not work as TreeView.SelectedNode is only a Get property)
Hope it helps.....