0
votes

I have a tree view that has some parent nodes and child nodes.I have four buttons. In which two buttons are of checked and unchecked only parent nodes and two buttons are of child nodes. I want checked only parent nodes if i click btnAllParentChecked and unchecked if i click btnAllparentUnchecked and similarly for child nodes.I have done some code for child nodes.

Boolean bChildTrigger = true;
        Boolean bParentTrigger = true;
        private void CheckAllChildren(TreeNodeCollection trNodeCollection, Boolean bCheck)
        {
            bParentTrigger = false;
            foreach (TreeNode ctn in trNodeCollection)
            {
                bChildTrigger = false;
                ctn.Checked = bCheck;
                bChildTrigger = true;

                CheckAllChildren(ctn.Nodes, bCheck);
            }
            bParentTrigger = true;
        }

 private void btnAllPropertyChecked_Click(object sender, EventArgs e)
        {
            CheckAllChildren(treSelector.Nodes, true);
        }

private void btnAllPropertyUnChecked_Click(object sender, EventArgs e)
        {
            CheckAllChildren(treSelector.Nodes, false);
        }

How can i implement this functionality in treeview?

1
Have you tried anything? Questions that doesn't show what the user has tried, usually gets closed.Mario S
Show your HTML markup.DevlshOne

1 Answers

1
votes

This is all you need. Tested and works well

private void ChangeNodesSelection(TreeNodeCollection node,bool doCheck)
    {
        foreach (TreeNode n in node)
        {
            n.Checked = doCheck;
            if (n.Nodes.Count > 0)
            {
                ChangeNodesSelection(n.Nodes,doCheck);
            }
        }
    }

private void UncheckParentNodes(TreeNodeCollection node)
    {
        foreach (TreeNode n in node)
        {
            if (n.Parent == null && n.Nodes.Count == 0)
                n.Checked = false;
        }
    }