How to set the SelectedNode and Set the Focus of the selected node in Telerik RadTreeView?

12,125

Solution 1

The .FindNodeByValue looks in the Nodes of the tree view. It doesn't look at each child node. The solution was to recursively walk tree. Here is my code that finally solved the issue:

    private void SelectLink(int linkID, RadTreeNodeCollection rootNodes)
    {
        var node = rootNodes.FindNodeByValue(linkID.ToString());
        if (node != null)
        {
            node.Selected = true;
            node.Expanded = true;
            node.ExpandParentNodes();
            node.Focus();

            ... Do some other work ...

            return;
        }

        // for each node with children  
        foreach (RadTreeNode item in rootNodes.Cast<RadTreeNode>().Where(item => item.Nodes.Count > 0))
        {
            // Recursive call to self to walk the tree
            SelectLink(linkID, item.Nodes);
        }
    }

I then simply call the method with the root RadTreeView:

SelectLink(radTreeViewMenuStructure.Nodes, idToFind);

Solution 2

You just need to also call node.ExpandParentNodes();

Share:
12,125
Rodney Hickman
Author by

Rodney Hickman

Highly Motivated and Experienced IT Professional with Expert Proficiency in Mobile and Web Development. Technical Talent and Business Expertise Transferable to Management-Level Positions Across Industries. Comprehensive understanding of the software development life cycle (SDLC) from requirements and technical specification definition to application design, development, and implementation. Regarded for the ability to drive processes and motivate cross-functional teams; works well under pressure to manage and meet multiple project deadlines. Objective and progressive with dynamic leadership, technical, and business acumen. Specialties IT Software Management, Staff Leadership and Development, Project Management, Business / Technical Requirements Development, Process Improvement / Cost Reduction, Client Relationship Management, Website Architect &amp; Design, SDLC, Business Process Management Systems, Information Architecture, Responsive Design, C#, ASP .Net, MVC, SQL, Entity Framework, Object Oriented Design and Programming (OOD, OOP), Agile with Scrum

Updated on June 26, 2022

Comments

  • Rodney Hickman
    Rodney Hickman almost 2 years

    I am using the Telerik RadTreeView with ASP .Net C#. I am able to set the Selected Node using the following code:

            var node = radTreeViewMenuStructure.Nodes.FindNodeByValue(linkID.ToString());
    
            if (node != null) // <- equals null when not on the root of the tree
            {
                node.Selected = true;
                node.Expanded = true;
                node.ExpandParentNodes();
                node.Focus();
            }
    

    The above code sets the selected node only if the node is just off the root and not enclosed in a parent node. My node = null when choosing an ID of a node that is enclosed within a parent node. Any suggestions?

  • Rodney Hickman
    Rodney Hickman about 12 years
    This was a great help and I added it to the code in my question. But it didn't solve my issue. Thanks for your input.
  • Darren
    Darren over 6 years
    What is the linkID you are using? It is readonly to assign an ID to a RadTreeNode.