C# looping and setting check status for treeview

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

JCWEBHOSTAsked:
Who is Participating?
 
mvdeveloperConnect With a Mentor Commented:
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
 
JCWEBHOSTAuthor Commented:
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
 
mvdeveloperCommented:
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
 
JCWEBHOSTAuthor Commented:
its is not picking up that it have children

treenode.ChildNodes.Count > 0

but it is there, please help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.