JCWEBHOST
asked on
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
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;
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
}
}
}
}
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
{
for (int k = 0; k < treenode.ChildNodes.Count;
{
TreeNode childNode = treenode.ChildNodes [ k ];
for (int i = 0; i < categories.Length; i++)
{
if (childNode.Value == categories[ i ].ToString())
{
childNode.Checked = true;
}
}
}
}
}
}
ASKER
its is not picking up that it have children
treenode.ChildNodes.Count > 0
but it is there, please help
treenode.ChildNodes.Count > 0
but it is there, please help
ASKER
string[] divisions = dt.Rows[0]["divisions_perm
string[] categories = dt.Rows[0]["categories_per
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
{
please help