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?

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;
    }
}

true if a check box is displayed next to each tree node in the tree view control; otherwise, false. The default is false. Examples. The following code example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. First, all the nodes are collapsed, and a handler is added for the BeforeExpand

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

' After a tree node's Checked property is changed, all its child nodes are updated to the same value. Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck ' The code only executes if the user caused the checked state to change.

For ASP.NET WEB APPLICATION:

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

Here Mudassar Ahmed Khan has explained with an example, how to get selected (checked) CheckBoxes of TreeView on Button click in ASP.Net using C# and VB.Net. When the Button is clicked, all the selected (checked) CheckBoxes are fetched and displayed using JavaScript Alert Message Box.

    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Node.Checked)
        {
            UncheckNodes(treeView1.Nodes,e.Node);
        }

    }

    private void UncheckNodes(TreeNodeCollection nodes, TreeNode except)
    {
        foreach (TreeNode node in nodes)
        {
            if (node != except) node.Checked = false;
            UncheckNodes(node.Nodes, except);
        }
    }

Here Mudassar Ahmed Khan has explained, how to check and uncheck all (select unselect or deselect all) child node CheckBox or CheckBoxes of ASP.Net TreeView control using JavaScript and jQuery. The idea is to attach the Client Side JavaScript click event handler using jQuery to all CheckBoxes inside the TreeView control and then when some CheckBox is clicked then based on whether it is Child

As I stumbled upon a similar issue I found a neat and simple solution. Yes, it's 8 years later, but maybe someone can make use of this.

Whenever a TreeNode is checked, store this TreeNode in a List. When you need to uncheck all nodes, loop the List and uncheck the nodes. This way you only iterate affected nodes.

Example:


Store TreeNodes in a List after check

public List<TreeNode> checkedNodes;

private void treeView1_afterCheck(object sender, TreeViewEventArgs e)
{
    foreach(TreeNode node in treeView1.Nodes)
    {
    if(node.checked) {
        checkedNodes.Add(node);
    }
}

Iterate only checked nodes to uncheck all

public void UncheckAllNodes()
{
    foreach (TreeNode node in checkedNodes)
    {
        node.Checked = false;
    }
}

This is still missing a removal when unchecking a checkbox, but you get the idea.

All TreeNodes will display checkboxes if the CheckBoxes property has been enabled. You can, however, disable a checkbox for a particular TreeNode by settings its Checkable property to "False". By default checking a node will not check the child nodes. You can enable this behavior by setting the CheckChildNodes property to "True"

Node CheckBox in WPF TreeGrid (SfTreeGrid) SfTreeGrid provides support for loading CheckBox in the expander cell of each node, which allows the user to check/uncheck the corresponding node. You can display check box in each node by setting SfTreeGrid.ShowCheckBox property as true.

Then, you need a class that represents the data for a single node in the tree. For example: internal class Node : ObservableObject { private ObservableCollection<Node> mChildren; // Add all of the properties of a node here. In this example, // all we have is a name and whether we are expanded.

N Method Name & Description 1 CollapseAll Collapses all the nodes including all child nodes in the tree view control. net, constructors in c# with examples, difference between virtual, override, new keywords in c# and many articles relating to asp. The purpose of this file is to show a few ways how the VBA Treeview can be used in a real life app.

Comments
  • This question was asked and answered over a year ago. How does your answer provide new information that hasn't been covered by the previous respondents?
  • Your checkedNodes list isn't initialized. Why are you looping in the AfterCheck event? You keep adding the same checked nodes over and over again. You don't need to, based on what you are trying to do. Also, you are only capturing root level nodes, not child nodes. Use the TreeViewEventArgs to only examine the node being checked.