Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

Lets say I have a treeview with parent nodes that have child nodes that have grandchild nodes.

The Question I have is lets say I select a grand child node. . . .How will I detect what that grand child nodes value is and the child and parent value is of the selected grandchild?


Thanks?
Posted

Assuming you have a TreeNode called myNode then the parent child relationship is simple:
TreeNode parent = myNode.Parent;
TreeNode grandParent = parent == null ? null : parent.Parent;
What kind of value are you talking about?


"Lets say i have a node named school and then that node has child nodes of class rooms and the classrooms has child nodes of desks.
In other words : School : class room 1 : desk 1
: desk 2
: class room 2 : desk 1
: desk 2
: class room 3 : desk 1
: desk 2
so lets say i select desk 2 of class room 3
I want to have the node values of desk 2 class room 3 and school
'Thanks"



The problem I have is that a TreeNode doesn't have a Value - it has a Text field, which would give you "desk 2" from both School:class room 1:desk 2 and School:class room 2:desk 2. But what you want sound like you have a Desk object which is associated with each of the desk nodes and which you want to retrieve via the tree. If so, then that is easy: each TreeNode has a Tag property which contains an object. If you assign your Desk instance to the appropriate TreeNode, you can retrieve it:
Desk desk = myNode.Tag as Desk;
if (desk != null)
   {
   ...
   }
 
Share this answer
 
v2
Comments
Gigantour 13-Jun-11 3:00am    
Lets say i have a node named school and then that node has child nodes of class rooms and the classrooms has child nodes of desks.

In other words : School : class room 1 : desk 1
: desk 2
: class room 2 : desk 1
: desk 2
: class room 3 : desk 1
: desk 2

so lets say i select desk 2 of class room 3

I want to have the node values of desk 2 class room 3 and school
'Thanks
OriginalGriff 13-Jun-11 3:27am    
Answer updated
Dalek Dave 13-Jun-11 3:51am    
Good Answer.
Just create a recursive function like that

private string SelectNode()
{
    return SelectNode(tvFileMaps.Nodes);
}
private string SelectNode(TreeNodeCollection tColl)
{
    foreach (TreeNode tn in tColl)
    {
        if (tn.IsSelected)
        {
            return tn.Text;
        }
        else
        {
            return SelectNode(tn.Nodes);
        }
    }
    return string.Empty;
}


This will return you the selected node value, if you want to return the parent node value then do some change in that
 
Share this answer
 
Comments
Dalek Dave 13-Jun-11 3:51am    
Good Call.
Hello Dear
treeview has a property SelectedNode which detemines which node in a treeview is selected if this property is null , it means no node has been selected otherwise selected node is returned
the SelectedNode is a TreeNode and has a lot of useful propery for example
level which detemines the level of node
parent which detemines parent of node
FullPath which is a string and detemines the unique path of node in ThreeView
and ...
 
Share this answer
 
Comments
OriginalGriff 13-Jun-11 3:20am    
This is not a criticism, just a "heads-up" that your greeting does not translate well into English: "Hello Dear" has sexist connotations in parts of the Western world which may be taken badly if the addressee is female. I would recommend that you just leave it off your messages in future!

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