Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check and uncheck all the nodes of the tree view in c#

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?

like image 569
Barbie Avatar asked May 27 '11 07:05

Barbie


2 Answers

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;
    }
}
like image 88
Olav Haugen Avatar answered Sep 20 '22 11:09

Olav Haugen


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);.

like image 21
FIre Panda Avatar answered Sep 20 '22 11:09

FIre Panda