Check and uncheck all tree nodes in C #

I have a tree view in a Windows application and a tree view with checkboxes, and I have some "parent nodes" and some "child nodes", and I want to CHECK AND LEARN the parent and child nodes at a time by clicking the "Check All" button and Remove All ... How do I do this?

+5
source share
3 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;
    }
}
+16
source

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.Nodesthis function, for example CheckUncheckTreeNode(trView.Nodes, true);, in a button click event to check all nodes. To uncheck, do CheckUncheckTreeNode(trView.Nodes, false);.

+2

ASP.NET WEB-:

Button_Click() {
    CheckUncheckTreeNode(YourTreeView.Nodes, false);
}

private void CheckUncheckTreeNode(TreeNodeCollection trNodeCollection, bool isCheck) {
    foreach (TreeNode trNode in trNodeCollection) {
        trNode.Checked = isCheck;
        if (trNode.ChildNodes.Count > 0)
            CheckUncheckTreeNode(trNode.ChildNodes, isCheck);
    }
}
0

All Articles