Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am using tree view first time in my application, I have bind it with database through following code :

C#
protected void PopulateTreeView(TreeNodeCollection parentNode, int parentID, DataTable folders)
        {
            foreach (DataRow folder in folders.Rows)
            {
                if (Convert.ToInt32(folder["parentID"]) == parentID)
                {
                    String key = folder["ID"].ToString();
                    String text = folder["title"].ToString();
                    TreeNodeCollection newParentNode = parentNode.Add(key, text).Nodes;
                    PopulateTreeView(newParentNode, Convert.ToInt32(folder["ID"]), folders);
                }
            }
        }


and write the following code on button click :

private void button1_Click(object sender, EventArgs e)
        {
            SqlDataAdapter adp = new SqlDataAdapter("viewMyTable", con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            adp.Fill(dt);
            PopulateTreeView(treeView1.Nodes, 0, dt);
        }


Now I want to check all the child node if the parent node of the children is checked and vice-versa.

Please help me, if somebody knows the answer.

Parveen Rathi
Posted
Updated 2-May-11 0:08am
v2

1 solution

You can try this (warning: I typed this off the top of my head so it may need some tweaks):

C#
// Prevent expansion of a node that does not have any checked child nodes.
private void TreeNode_Clicked(object sender, TreeViewCancelEventArgs e)
{
    bool checkChildren = (e.Node.Checked);
    if (node.Nodes.Count == 0) 
    {
        return;
    }
    foreach (TreeNode childNode in e.Node.Nodes)
    {
        childNode.Checked = !childNode.Checked;
    }
}
 
Share this answer
 
Comments
Parveen Rathi 2-May-11 8:03am    
Thanks for your reply my prolem has been solved
Parveen Rathi 2-May-11 8:05am    
Sir plz tell me how to get name of the node which i am checkec or find which type of node i am checkec wheter parent or child

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