Searching deep into Telerik Radtreview subtrees for a node with given value?

StackOverflow https://stackoverflow.com/questions/13533512

  •  01-12-2021
  •  | 
  •  

سؤال

I have a RadTreeView C# component. The tree is nested, so some Nodes have their sub-trees, stored in Nodes property of upper-level Nodes.

Now I need to find a node by value. Node is hidden somewhere in subtrees. If I use call

  RadTreeNode rtn= PagesTreeView.Nodes.FindNodeByValue(i.ToString());

where PagesTreeView is my tree, then it searches only across top-level nodes.

How I can Find Node by Value using not only Nodes from the current level of tree, but also dive into subtrees? Do I need to write such recursive search myself or there is a straightforward solution?

هل كانت مفيدة؟

المحلول 2

Yes, you would need to write your own recursive function to do the search. Another option if you are using the ASP.NET AJAX version of the control is the GetAllNodes() method. It returns all nodes in the tree hierarchy (I'm guessing it uses recursion under the hood). Once you have the entire list you would search it for the node you care about. The big drawback with that approach is if you have a lot of nodes the search could be slow and consume a lot of memory. Doing your own recursive search is the best approach.

See this article for more info.

نصائح أخرى

Recursively searching the RadComboBox

There isn't a built in function to recursively search, however you can roll your own pretty easiliy. This should work for you (not tested):

RadTreeNode FindNodeRecursive(RadTreeNodeCollection nodes, string value)
{
    foreach (RadTreeNode node in nodes)
    {
        if(node.Value == value)
            return node;

        if (node.Nodes.Count > 0)
        {
            FindNodeRecursive(node.Nodes, value);
        }

        return null;
    }
}

And then call it like this:

var node = FindNodeRecursive(PagesTreeView.Nodes, i.ToString());

Old question but I faced this same problem in my application, how to search through the hierarchical tree nodes.

I would like to share one correction to previous solutions proposal. When calling FindNodeRecursive() recursively the return value of your recursive call is never being evaluated or assigned to a variable. So, you will always end up with going through the foreach loop and return value is null.

Corrected and tested function code (WPF C#):

    RadTreeNode FindNodeRecursive(RadTreeNodeCollection nodes, string value)
    {
        RadTreeNode ret = null;
        foreach (RadTreeNode node in nodes)
        {
            if(node.Value == value)
                return node;

            if (node.Nodes.Count > 0)
            {
                ret = FindNodeRecursive(node.Nodes, value);
            }

            return ret;
        }
    }

Function use:

    var node = FindNodeRecursive(PagesTreeView.Nodes, i.ToString());
    if (node != null) // found 
    { 
        ;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top