TreeView Filtering Child nodes question

4 posts, 0 answers
  1. Ron Weingarten
    Ron Weingarten avatar
    5 posts
    Member since:
    May 2010

    Posted 18 Nov 2013 Link to this post

    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.
  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 21 Nov 2013 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Bilal
    Bilal avatar
    2 posts
    Member since:
    Sep 2013

    Posted 10 Jul 2014 in reply to Dess Link to this post

    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,
  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 14 Jul 2014 Link to this post

    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.
     
Back to Top