简体   繁体   中英

how to get treeview control checkboxes name in c#

I have created a treeview with parent and childnodes created dynamically.Also have enabled checkbox property true.hence got check boxes for each node. The problem is how to name these check boxes so that for a particular user if the value is true, the checkbox should be checked, else if the value is false the checkbox needs to be unchecked. true or false value is stored in a particular column in a db.

Please follow the code I've provided it might give you some Idea from what I understand from your question. I've provided the link as well. http://msdn.microsoft.com/en-us/library/wwc698z7(v=vs.90).aspx

private void PrintRecursive(TreeNode treeNode)
{
   // Print the node.
System.Diagnostics.Debug.WriteLine(treeNode.Text);
MessageBox.Show(treeNode.Text);
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
  { 
    PrintRecursive(tn);
  }
 }

// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
// Print each node recursively.
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
  {
    PrintRecursive(n);
  }
}

you can check which node is checked by the following code

foreach (TreeNode node in yourtreeview.Nodes)
        {
            if (node.Checked)
            { //here You can check here your parent nodes is checked or not 
                //your calculations 
            }
foreach (TreeNode ChildNode in node.Nodes)
            {
                if (ChildNode.Checked)
                { // here you can check your 2nd level nodes
                }
foreach (TreeNode childofChild in ChildNode.Nodes)
                {
                    if (childofChild.Checked)
                    { here you can check your 3rd level node }
 foreach (TreeNode GrandChildofChild in childofChild.Nodes)
                    {
                        if (GrandChildofChild.Checked)
                        {
                            //here you can check your fourth level node
                        }
                     }
                }
        }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM