Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
VB
- 1
     1.1
     1.2
   - 1.3
         1.3.1
         1.3.2
        - 1.3.3
            1.3.3.1
            1.3.3.2
+ 2
+ 3

Hi All,

For example if we consider that the above mentioned is like Treeview,Here total 5 parent nodes are there those are
1,
1.3,
1.3.3,
2,
3

For Parent Nodes Like,

1
1.3
1.3.3

I wrote 3 for each loop’s if you observe in my code.
C#
protected void tvCategories_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        try
        {
            foreach (TreeNode treenode in tvCategories.Nodes)
            {
                if (treenode.Text == e.Node.Text)
                {
                    treenode.Expand();
                    treenode.ImageUrl = "~/Images/OpenedBookColorInBlue.png";
                    break;
                }
                foreach (TreeNode t in treenode.ChildNodes)
                {
                    if (t.Text == e.Node.Text)
                    {
                        t.Expand();
                        t.ImageUrl = "~/Images/OpenedBookColorInBlue.png";
                        break;
                    }
                    foreach (TreeNode t1 in t.ChildNodes)
                    {
                        if (t1.Text == e.Node.Text)
                        {
                            t1.Expand();
                            t1.ImageUrl = "~/Images/OpenedBookColorInBlue.png";
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex.ToString());
        }
    }

protected void tvCategories_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        try
        {
            foreach (TreeNode treenode in tvCategories.Nodes)
            {
                if (treenode.Text == e.Node.Text)
                {
                    treenode.Collapse();
                    treenode.ImageUrl = "~/Images/ClosedBookColorInBlue.png";
                    break;
                }
                foreach (TreeNode t in treenode.ChildNodes)
                {
                    if (t.Text == e.Node.Text)
                    {
                        t.Collapse();
                        t.ImageUrl = "~/Images/ClosedBookColorInBlue.png";
                        break;
                    }
                    foreach (TreeNode t1 in t.ChildNodes)
                    {
                        if (t1.Text == e.Node.Text)
                        {
                            t1.Collapse();
                            t1.ImageUrl = "~/Images/ClosedBookColorInBlue.png";
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex.ToString());
        }
    }


Now For Example If my Treeview is like below mentioned,How many forech loops i want to write??

VB
- 1

    1.1

    1.2

   - 1.3

           1.3.1

           1.3.2

         - 1.3.3

                1.3.3.1

                1.3.3.2

                - 1.3.3.3

                       1.3.3.3.1

                       1.3.3.3.2

                       - 1.3.3.3.3

                              1.3.3.3.3.1
+ 2

+ 3


According to my code for the above mention treeview I want to write 5 for each loop’s
Suppose If i have n-parent nodes what can i do????
If i increase my code like that it will be bad & it gets too much lenthy code

so it is not correct naa.

So what was the solution for this???

Please give me...

Thanks in Advance,
VenkiDesai.k
Posted
Updated 8-Nov-12 17:34pm
v4
Comments
manognya kota 8-Nov-12 8:45am    
How are you populating your tree view?Is it from database or
dynamic ?
Venki Desai 9-Nov-12 0:11am    
From Database

1 solution

hi,

this link will help you.

Loading a treeview to n levels[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900