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

TreeView Filtering Child nodes question

6 Answers 445 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Ron Weingarten
Top achievements
Rank 1
Ron Weingarten asked on 18 Nov 2013, 05:18 PM
I have a TreeView that I sometimes set the Filter property of. Most of my top level nodes have child nodes; but when I use the Filter, child nodes that don't match the Filter are not shown underneath the parent nodes that DO match the filter.

Is there a way to not hide the child nodes of a parent node that matches the filter when the child nodes do not?

Edit: I discovered custom filtering and wrote this as my predicate:

private bool FilterNode(Telerik.WinControls.UI.RadTreeNode oNode)
        {
            if (oNode.Text.ToLower().Contains(txtSearch.Text.ToLower()))
                return true;
            else if (oNode.Parent != null)
                return (FilterNode(oNode.Parent));
            else
                return false;
        }

Does what I need.

6 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Nov 2013, 11:41 AM
Hello Ron,

Thank you for contacting Telerik Support.

RadTreeView supports filtering of its nodes according to their Text property. If a certain node does not match the filter, it is not shown, no matter that it is a child node or parent. If your requirement is to apply the filter only for the parent nodes, Custom filtering, as you have already found out, is a flexible mechanism for filtering RadTreeView nodes by using custom logic:
public Form1()
{
    InitializeComponent();
 
    radTreeView1.Filter = "Parent";
 
    radTreeView1.TreeViewElement.FilterPredicate = FilterNode;
}
 
private bool FilterNode(RadTreeNode node)
{
    if (node.Parent != null)
    {
        return FilterNode(node.Parent);
    }
    if (node.Text.ToLower().Contains(node.TreeViewElement.Filter.ToString().ToLower()))
    {
        return true;
    }
 
    return false;
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Bilal
Top achievements
Rank 1
answered on 10 Jul 2014, 09:21 AM
Hello,

I tried to use the approach but I could not find FilterPredicate property!!
I am using version number V2.0.50727
It is property supported in this version? if not is there any other way to filter the nodes with keeping the children shown in the tree?

Regards,
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Jul 2014, 03:10 PM
Hello Bilal,

Thank you for writing.

FilterPredicate provides a callback so that the default filtering expression parser can be substituted. It is supported in .NET 2.0 as well. I would like to note the provided sample code is applicable for all versions since Q3 2011 SP1 (version 2011.3.11.1219), where the support for custom filtering was introduced, to the latest version. Only the Custom filtering functionality gives you to opportunity to implement your own logic for filtering the nodes.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Brian
Top achievements
Rank 1
answered on 22 Jun 2017, 01:56 AM

I know this is old but I am trying do something similar except that I need all children of a matched filter and direct parents to show. Your example works for matching top level but I want to match all levels then traverse up and down showing the complete matched hierarchy.  

Example:

Search: Nike

-shoes
  --nike
     --Air Jordans

 

0
Brian
Top achievements
Rank 1
answered on 22 Jun 2017, 02:25 AM
Disregard I found another post with an example.  
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 26 Jun 2017, 09:37 AM
Hello Brian, 

Thank you for writing back. 

I am glad that you have found a suitable solution for your case. Note that the custom filtering mechanism is quite useful to control which node to be visible or not considering its parent node or child nodes. Additional information is available here: http://docs.telerik.com/devtools/winforms/treeview/working-with-nodes/custom-filtering

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Treeview
Asked by
Ron Weingarten
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Bilal
Top achievements
Rank 1
Brian
Top achievements
Rank 1
Share this question
or