Rodney Hickman
Rodney Hickman

Reputation: 3143

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

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?

Upvotes: 3

Views: 12573

Answers (3)

BrianK
BrianK

Reputation: 2685

You just need to do: radTreeViewMenuStructure.FindNodeByValue() that will serch the whole tree.

Upvotes: 0

Rodney Hickman
Rodney Hickman

Reputation: 3143

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);

Upvotes: 3

lukiffer
lukiffer

Reputation: 11293

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

Upvotes: 1

Related Questions