Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-19
10
Medium Priority
?
1,871 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 2000 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
Independent Software Vendors: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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