Im using following code:
TreeNode i = treeView1.SelectedNode;
RefillTree(); //clears the tree and rebuilt it again.
treeView1.SelectedNode=i;
However, SelectedNode still remains null, however “i” is correctly referencing.
I would need to select and expand particular node automatically after tree refresh.
Thank you
What does “RefillTree” do exactly? If it removes the Node referenced by ‘i’, then I would expect that setting the SelectedNode property to a node that does not exist in the control would have no effect.
EDIT:
I can almost guarantee that you are clearing the control and creating new nodes to fill it with. It does not matter if these new nodes contain the same data, SelectedNode looks for object equality and it doesn’t find a match. for example, this code reproduces your problem:
So, you can instead find the node by value after clearing the control:
Using LINQ here seems clunky, but the TreeNodeCollection class only exposes a Find() method which uses the node’s Name property. You could also use that, but equally clunky.