Are there more straight forward method than the code below to get the root nodes or the first level nodes in a tree view?
TreeNode node = treeView.SelectedNode;
while(node != null)
{
node = node.Parent;
}
Actually the correct code is:
TreeNode node = treeView.SelectedNode;
while (node.Parent != null)
{
node = node.Parent;
}
otherwise you will always get node = null
at the end of the loop.
BTW, if you are sure to have one and one only root in your TreeView
, you could consider to use directly treeView.Nodes[0]
, because in that case it would give the root.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With