Reputation: 2503
I have a custom tree view inherited from asp.net tree view control. with nth level parent- child relationship. based on some calculation I have checked child node. I want parent node should be checked if all the child node are checked. As I am checking child nodes on based some calculation so I can't use after check event. can some one provide me C# code for that?
private TreeNode _parentNode;
private void CheckedParent(TreeNodeCollection nodeCollection)
{
foreach (TreeNode node in nodeCollection)
{
if (node.ChildNodes.Count > 0)
{
_parentNode = node;
CheckedParent(node.ChildNodes);
}
else
{
bool allChildChecked = true
foreach (TreeNode childNode in nodeCollection)
{
if (!childNode.Checked)
{
allChildChecked = false;
}
}
}
}
if (allChildChecked )
{
_parentNode.Checked = true;
_isAllChildChecked = false;
}
}
Upvotes: 3
Views: 4464
Reputation: 21365
This method will return true
if all child nodes are checked; otherwise it will return false
private bool AllChildChecked(TreeNode currentNode)
{
bool res = true;
foreach (TreeNode node in currentNode.ChildNodes)
{
res = node.Checked;
if (!res) break;
res = this.AllChildChecked(node);
if (!res) break;
}
return res;
}
Upvotes: 2