Click here to Skip to main content
15,895,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,
I have a treeview in my windows application.

in this treeview users can add some root nodes and also some sub nodes for these root nodes and also some sub nodes for these sub nodes and so on.

I mean something like this :

Root1
     A
       B
         C
         D
          E  
Root2
     F
      G
.
.
.

Now my question is that if i have 'E' node what is the best way to find its first root node ('Root1')?
Posted

Each TreeNode has a Parent property just recurse upwards until the Parent is null.
 
Share this answer
 
Comments
Amir Mahfoozi 26-Nov-11 4:31am    
+5 I agree with you
C#
private TreeNode FindRootNode(TreeNode treeNode)
      {
          while (treeNode.Parent != null)
          {
              treeNode = treeNode.Parent;
          }
          return treeNode;
      }
 
Share this answer
 
Comments
CHill60 21-Jun-13 12:14pm    
That's essentially what Mehdi Gholam said a year and a half ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900