Solved

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

Posted on 2007-03-19
10
1,782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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