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;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Actually the correct code is:
otherwise you will always get
node = nullat 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 directlytreeView.Nodes[0], because in that case it would give the root.