Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all...
I have the treeview in win form which is dynamically loaded. In this treeview, I will get checkboxes along with each node...
Now, if i selected the parent node then the childnodes under the selected nodes should also be selected.
How to write code for this. Please suggest me.
Thank you.

[Removed unneeded pre tags]
Posted
Updated 28-Sep-10 3:08am
v2

C#
private void addParentNode_Click(object sender, EventArgs e) {
  treeView2.BeginUpdate();
  //treeView2.Nodes.Clear();
  string yourParentNode;
  yourParentNode = textBox1.Text.Trim();
  treeView2.Nodes.Add(yourParentNode);
  treeView2.EndUpdate();
}
private void addChildNode_Click(object sender, EventArgs e) {
  if (treeView2.SelectedNode != null) {
    string yourChildNode;
    yourChildNode = textBox1.Text.Trim();
    treeView2.SelectedNode.Nodes.Add(yourChildNode);
    treeView2.ExpandAll();
  }
}

private void addChildNode_Click(object sender, EventArgs e) {
  var childNode = textBox1.Text.Trim();
  if (!string.IsNullOrEmpty(childNode)) {
    TreeNode parentNode = treeView2.SelectedNode ?? treeView2.Nodes[0];
    if (parentNode != null) {
      parentNode.Nodes.Add(childNode);
      treeView2.ExpandAll();
    }
  }

}

Thanks & Regard Aarti.Thakkar (nemo)
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...:thumbsup::rose:
 
Share this answer
 
v2
Comments
Ramesh Jallepalli 29-Sep-10 1:43am    
thank you... this code is not working properly and showing errors like two same methods are defined. how to modify it. or please modify my code bcaz it is missing small logic.
hi...

I wrote the code for this as below but some logic is missed. How to add that logic please let me know.. or you can give me your own code

-------------------------------------------------
Boolean bChildTrigger = true;
       Boolean bParentTrigger = true;

       private void trView_AfterCheck(object sender, TreeViewEventArgs e)
       {
         
           if (bChildTrigger)
           {
               CheckAllChildren(e.Node, e.Node.Checked);
           }
           if (bParentTrigger)
           {
               CheckMyParent(e.Node, e.Node.Checked);
           }

       }

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

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

                void CheckMyParent(TreeNode tn, Boolean bCheck)
                {
                    if (tn == null) return;
                    if (tn.Parent == null) return;

                    bChildTrigger = false;
                    bParentTrigger = false;
                    tn.Parent.Checked = bCheck;
                    CheckMyParent(tn.Parent, bCheck);
                    bParentTrigger = true;
                    bChildTrigger = true;
                }
----------------------------------------------------------------
<pre>
 when I selected the parent node, automatically the child nodes are selected and the root node is also selected. But the problem is when I deselect the any one of the selected child nodes then the Parent node and Root node are also getting deselected state even some child nodes are already in selected state.
please solve this problem

thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900