This is a migrated thread and some comments may be shown as answers.

Select the first leaf node of a tree

2 Answers 83 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nataraj Vedula
Top achievements
Rank 1
Nataraj Vedula asked on 05 Nov 2008, 12:41 PM
Hi,

I have a Rad tree view bound to a dataset. When page loads, i want to display the tree view with the first leaf node in the tree structure. for Eg:
Consider tree has the following structure:
A:-
    A1:-
        A11:-
            A111
            A112
            A113
        A12:-
            A121
            A122
        A13
    A2:-
        A21:-
            A211
            A212:-
                A2121
        A22
    A3
B:-
    B1:-
    so on...

when page loads i want to show the user node A11 selected and also parent nodes for A11 expanded.(if A11 has no child nodes then A11 has to be selected as it will be first leaf node) How can i do this server side?

I tried to do it by loops but i am not successful to traverse through the nodes at diefferent levels.

Please help me.

Thanks in advance,
Nataraj

2 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 05 Nov 2008, 03:30 PM
Hello Nataraj Vedula,

You can use the following routines to achieve your goal:

[C#]
private void LocateFirstLeafNode() 
    RadTreeNode node = GetFirstLeafNode(this.RadTreeView1.Nodes); 
 
    if (node != null
    { 
        node.Selected = true
 
        node.ExpandParentNodes(); 
    } 
 
private RadTreeNode GetFirstLeafNode(RadTreeNodeCollection nodes) 
    RadTreeNode firstLeafNode = null
 
    if (nodes.Count > 0) 
    { 
        if (nodes[0].Nodes.Count == 0) 
        { 
            firstLeafNode = nodes[0]; 
        } 
        else 
        { 
            firstLeafNode = GetFirstLeafNode(nodes[0].Nodes); 
        } 
    } 
 
    return firstLeafNode; 

[VB.NET]
Private Sub LocateFirstLeafNode() 
    Dim node As RadTreeNode = GetFirstLeafNode(Me.RadTreeView1.Nodes) 
 
    If node IsNot Nothing Then 
        node.Selected = True 
 
        node.ExpandParentNodes() 
    End If 
End Sub 
 
Private Function GetFirstLeafNode(ByVal nodes As RadTreeNodeCollection) As RadTreeNode 
    Dim firstLeafNode As RadTreeNode = Nothing 
 
    If nodes.Count > 0 Then 
        If nodes(0).Nodes.Count = 0 Then 
            firstLeafNode = nodes(0) 
        Else 
            firstLeafNode = GetFirstLeafNode(nodes(0).Nodes) 
        End If 
    End If 
 
    Return firstLeafNode 
End Function 

I hope this is useful.

Best wishes,
Simon
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nataraj Vedula
Top achievements
Rank 1
answered on 06 Nov 2008, 05:26 AM
It works great. Thanks Simon.

Regards,
Nataraj
Tags
TreeView
Asked by
Nataraj Vedula
Top achievements
Rank 1
Answers by
Simon
Telerik team
Nataraj Vedula
Top achievements
Rank 1
Share this question
or