?
Solved

C# looping and setting check status for treeview

Posted on 2012-08-29
4
Medium Priority
?
1,036 Views
Last Modified: 2012-08-30
hey guys i need to set check for the tree node values which i have stored in my database

i have managed to set the parent node but the child node i can not seem to set.

please help.

here my code

the divisions array is my parent values and categories and my child values

string[] divisions = dt.Rows[0]["divisions_permissions"].ToString().Split(new char[] { ',' });
                    string[] categories = dt.Rows[0]["categories_permissions"].ToString().Split(new char[] { ',' });

                    foreach (TreeNode treenode in TreeView.Nodes)
                    {
                        for (int f = 0; f < divisions.Length; f++)
                        {
                            if (treenode.Value == divisions[f].ToString())
                            {
                                treenode.Checked = true;
                            }
                        }

                        if (treenode.ChildNodes.Count > 0)
                        {
                            for (int k = 0; k < treenode.ChildNodes.Count; k++)
                            {
                                for (int i = 0; i < categories.Length; i++)
                                {
                                    if (treenode.Value == categories[i].ToString())
                                    {
                                        treenode.Checked = true;
                                    }
                                }
                            }
                        }

Open in new window

0
Comment
Question by:JCWEBHOST
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
mvdeveloper earned 1000 total points
ID: 38344514
Do you mean:

if (treenode.ChildNodes.Count > 0)
                        {
                            for (int k = 0; k < treenode.ChildNodes.Count; k++)
                            {
                                for (int i = 0; i < categories.Length; i++)
                                {
                                    if (treenode.ChildNodes[k].Value == categories[ i ].ToString())
                                    {
                                        treenode.Checked = true;
                                    }
                                }
                            }
                        }
0
 

Author Comment

by:JCWEBHOST
ID: 38344531
i have a values stored in my database

string[] divisions = dt.Rows[0]["divisions_permissions"].ToString().(new char[] { ',' });
string[] categories = dt.Rows[0]["categories_permissions"].ToString().Split(new char[] { ',' });

now the divisions values =[1,2]   and categories categories values= [4]

now in my treeview the parent node must equal to the value in my divisions array, if so the checkbox must be checked

my problem is the child not been checked

i did a trace and it does not even go into this:

if (treenode.ChildNodes.Count > 0)
                        {


please help
0
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 38344571
Hi

then your nesting is also wrong..

foreach (TreeNode treenode in TreeView.Nodes)
                    {
                        for (int f = 0; f < divisions.Length; f++)
                        {
                            if (treenode.Value == divisions[f].ToString())
                            {
                                treenode.Checked = true;
                         

                        if (treenode.ChildNodes.Count > 0)
                        {
                            for (int k = 0; k < treenode.ChildNodes.Count; k++)
                            {
                                TreeNode childNode = treenode.ChildNodes [ k ];
                                for (int i = 0; i < categories.Length; i++)
                                {
                                    if (childNode.Value == categories[ i ].ToString())
                                    {
                                        childNode.Checked = true;
                                    }
                                }
                            }
                        }
                     }
                }
0
 

Author Comment

by:JCWEBHOST
ID: 38344581
its is not picking up that it have children

treenode.ChildNodes.Count > 0

but it is there, please help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question