Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello
 
Before I have a question about filtering tree view and returning just frst match.
 
So I try to create list<treenode> and change the code, is it possible improve the below code for finding nodes and child nodes that match my criteria.
 
private List<TreeNode> FindNodeByValue(TreeNodeCollection nodes, string searchstring)
    {
       
        // Loop through the tree node collection
        List<TreeNode> nodelist=new List<TreeNode>();
 
        foreach (TreeNode node in nodes)
        {
            // Does the value match the search string?
            if (node.Value.ToUpper().Contains (searchstring.ToUpper()))
                // Yes it does match - return it
                nodelist.Add(node);
            //  return nodelist;
            else
            {
                // No it does not match - search any child nodes of this node
                List<TreeNode> childNode = SearchChildNodes(node, searchstring);
                // If the childNode is not null it was a match
                if (childNode != null)
                    // Return the matching node
                    return childNode;
            }
        }
        // If the matching node is not found return null
        return null;
    }
 
    /// <summary>
    /// This method searches a node's ChildNodes collection to find a matching value
    /// with the incoming search string
    /// It will iteratively call itself as it drills into each nodes child nodes (if present)
    /// </summary>
    /// <param name="parentNode">Parent node to search for a match</param>
    /// <param name="searchstring">string to be matched with the Nodes Value property</param>
    /// <returns>Treenode of the matching node if found.  If not found it will be null</returns>
    private List<TreeNode> SearchChildNodes(TreeNode parentNode, string searchstring)
    {
        List<TreeNode> nodelist2 = new List<TreeNode>();
        // Loop through the child nodes of the parentNode passed in
        foreach (TreeNode node in parentNode.ChildNodes)
        {
            // Does the value match the search string?
            if (node.Value.ToUpper().Contains(searchstring.ToUpper()))
                // Yes it does match - return it
                nodelist2.Add(node);
                //return nodelist2;
                //return node;
            else
            {
                // No it does not match - recursively search any child nodes of this node
                List<TreeNode> childNode = SearchChildNodes(node, searchstring);
                // If the childNode is not null it was a match
                if (childNode != null)
                    childNode.Add(node);
                    // Return the matching node
                    return childNode;
            }
        }
        // If the matching node is not found OR if there were no child nodes then return null
        return null;
    }
 

    protected void Button1_Click(object sender, EventArgs e)
    {
        TreeNode trnode=FindNodeByValue(TreeView1.Nodes, fieldFilterTxtBx.Text);
        if (trnode != null)
        {
            TreeView1.Nodes.Clear();
          //  TreeNode newnode = new TreeNode("Detail Engineering");
           // TreeView1.Nodes.Add(newnode);
            TreeView1.Nodes.Add(trnode);
            TreeView1.ExpandAll();
        }
        else
 
{
 
    Label1.Text = "No file found";
 
}
    }
Posted 26-Nov-12 3:09am
Edited 26-Nov-12 3:48am
RyanDev129.8K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use Linq (see example here: example[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100