Solved

Need help checking and unchecking child and parent nodes in treeview..using c#

Posted on 2007-03-19
10
1,703 Views
Last Modified: 2011-04-14
I am trying to firgure out a method to check all child nodes if a parent node is checked...BUT uncheck all parent nodes if a child node is unchecked without changing the siblings or other child nodes.

Just to make sure this is making sense..

If I check a node any child node of that node is autmoatically checked.

HOwever if I uncheck a node...sibling and child nodes are NOT unchecked..but all parent nodes are.

I am guessing the basics of the method would look like this..but of course I cant figure out the code to check and uncheck child and parent nodes.

Anyone able to help me out with the code?...has to be c#

protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        if (e != null)
        {
            if (e.Node.Checked == true)
            {

            }
            else
            {

            }
        }
        else
        {
        }
    }
0
Comment
Question by:Prysson
  • 5
  • 5
10 Comments
 
LVL 3

Expert Comment

by:Kate12
ID: 18753721
Just create a recursion method to check all nodes within the node path that was changed:
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeViewEventArgs e)
{
    if (e != null)
    {
        this.ChangeState(e.Node, e.Node.Checked);
    }
}

private void ChangeState(TreeNode node, bool shouldCheck)
{
    node.Checked = shouldCheck;
    foreach (TreeNode child in node.nodes)
    {
        child.Checked = shouldCheck;
        if (child.Nodes.Count > 0)
        {
            this.Changed(child, shouldCheck);
        }
    }
HTH
0
 

Author Comment

by:Prysson
ID: 18755471
Kate
Your code doesnt compile.

The ChangedState method line

foreach (TreeNode child in node.nodes)

the .nodes has a does not contain definition error


the same error with the Nodes in

 if (child.Nodes.Count > 0)

and finally the same error with

this.Changed(child, shouldCheck);

also I dont think your example gets me what I am looking for. Your code appears to uncheck child noes if the parent node is uncheck but that is not what I want to have occur. I want child nodes to be checked if the parent is checked...but I want the parent nodes to be unchecked if a child node is unchecked.


0
 
LVL 3

Accepted Solution

by:
Kate12 earned 500 total points
ID: 18755521
   protected void TreeView1_TreeNodeCheckChanged(object sender, TreeViewEventArgs e)
        {
            if (e != null)
            {
                if (e.Node.Checked)
                {
                    this.CheckAllChildNodes(e.Node);
                }
                else
                {
                    this.UncheckFromNodeToRoot(e.Node);
                }
            }
        }

        private void UncheckFromNodeToRoot(TreeNode treeNode)
        {
            treeNode.Checked = false;
            if (treeNode.Parent != null)
            {
                this.UncheckFromNodeToRoot(treeNode.Parent);
            }
        }

        private void CheckAllChildNodes(TreeNode node)
        {
            node.Checked = true;
            foreach (TreeNode child in node.Nodes)
            {
                child.Checked = true;
                if (child.Nodes.Count > 0)
                {
                    this.UncheckChildNodes(child);
                }
            }
        }

HTH
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Prysson
ID: 18755670
Kate12...

Your code seems to be close but there are stil some compile errors where I cant figure out what you are trying to do..

Specifically in the CheckAllChildNodes method.

twice you use node.Nodes however that give an error

'System.Web.UI.WebControls.TreeNode' does not contain a definition for 'Nodes'

Also you reference  an UncheckChildNodes(child) but UncheckChildNodes isnt a method. I am assuming you mean to call CheckAllChildNodes but cant be sure.

btw thanks for your help.
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755718
Yes i meant to do the recursionthere so it is not UncheckChildNodes but CheckAllChildNodes instead. Also i misunderstood that you are running this in ASP.NET and not Windows Forms. so instead of using te "Nodes" property you have to replace it by "ChildNodes" properties.

HTH
0
 

Author Comment

by:Prysson
ID: 18755734
One other things..am I using the correct event handler for recursivley checking child nodes?

I assumed that the compile errors were meant to be node.ChildNodes and that you meant to call the method itself with the CheckAllChildNodes..if I make those changed it compiles..but of course nothing actually happens..he he...I put in debugging stops to check the code..and it never runs teh code..so I am wondering is I have the right event handler chosen or not.

0
 

Author Comment

by:Prysson
ID: 18755761
Kate I am going to accept you solution...the code looks like it will do exactly what I want I just seem to be having some wiring issue or something with the event handler.

I thank you for the help.

Does the TheeNodeCheckChanged seem to you like the appropriate event handler for running this code?
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755797
As far as i can tell TreeNodeCheckChanged is the right handler for doing what you need.
Let me know how it goes.
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755887
A quick fix on firring the event. Just by checking the tree it will not cause to postback so you will have to update the tree between post-backs or you can force the post-back, here is an article showing how to do it:
http://www.developerfusion.co.uk/forums/thread/121579/?Ajax_CallBack=true
0
 

Author Comment

by:Prysson
ID: 18756760
Kate12 If you woudl like to go post this at my other question on this very issue over here

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22460342.html

That way I can give you the points since this is basically the solution I need for that question.

Thanks for looking into this for me.

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question