Saturday, January 8, 2011

Search Tree view Recursively and check the checkbox of search item

There is FindNode(searchval) method but it only finds with in level1 . it doesn’t find from the child nodes.



So here is code snippet for searching all the items of treeview.







    protected void Button1_Click(object sender, EventArgs e)

    {

         //search item

        string checkit = "item1";

      

         // iterating all parent nodes of treeview.

        foreach (TreeNode in TreeView1.Nodes)

        {

            search_it(t, str);

        }

    }



    protected void search_it(TreeNode t, string str)

    {

         if (t != null)

        {

             if (t.Value == str)

             {

                 t.Checked = true;

             }

             else

             {

                 // if node contains child node,then process it

                 foreach (TreeNode temp in t.ChildNodes)

                {           search_it(temp, str);



                }



            }



        }

        else

        {

             return;

         }



   }

No comments:

Post a Comment