I have the tree view in my windows application and in the tree view with the checkboxes and I have the Some "parent nodes" and some "child nodes" and i want to CHECK AND UNCHECK the parent and child nodes at a time on clicking the "Check All" and "Uncheck All" button... How should i do this?
Try something like this:
public void CheckAllNodes(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
node.Checked = true;
CheckChildren(node, true);
}
}
public void UncheckAllNodes(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
node.Checked = false;
CheckChildren(node, false);
}
}
private void CheckChildren(TreeNode rootNode, bool isChecked)
{
foreach (TreeNode node in rootNode.Nodes)
{
CheckChildren(node, isChecked);
node.Checked = isChecked;
}
}
Try
private void CheckUncheckTreeNode(TreeNodeCollection trNodeCollection, bool isCheck)
{
foreach (TreeNode trNode in trNodeCollection)
{
trNode.Checked = isCheck;
if (trNode.Nodes.Count > 0)
CheckUncheckTreeNode(trNode.Nodes, isCheck);
}
}
Pass treeView.Nodes
to this function like CheckUncheckTreeNode(trView.Nodes, true);
in button click event for checking all nodes. To uncheck all do CheckUncheckTreeNode(trView.Nodes, false);
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With