Click here to Skip to main content
15,900,616 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone...

I have populated a treeview. There are child nodes and those also have the child nodes and so on. Now at the time of edit, I am getting the datatable containing the treeview node values. and on the basis of that I need to make "checked" the check boxes at the time of page load. I am getting the Parent nodes only by using the following code below. But unable to get the child nodes.

DataTable dt = ds1.Tables[0];
foreach (TreeNode node in TreeViewArticleCatagory.Nodes)
{
    foreach (DataRow dr in dt.Rows)
    {
        if (Convert.ToString(dr["Item"]) == node.Value)
        {
            node.Checked = true;
            //TreeViewArticleCatagory.SelectedNode.Checked = true;
        }
    }
}

Please help me....

Thanks in advance.....

[edit]Code block inserted to preserve formatting - OriginalGriff[/edit]
Posted
Updated 24-Aug-10 22:42pm
v2
Comments
OriginalGriff 25-Aug-10 5:07am    
"It is not working...
I tried.."
What did you try? Edit your original question to show what you tried that uses node.Nodes, and add another comment to my answer to let me know. Do not create a new answer - I don't get told about them, so may not respond at all quickly!
arindamrudra 25-Aug-10 6:40am    
yes finally I got it...
arindamrudra 25-Aug-10 6:44am    
I used the recursive function for that....

ds1 = new DataSet();
ds1 = oCategory.GetCategoriesOfArticle(ArticleID);
if (ds1 !=null && ds1.Tables[0].Rows.Count > 0)
{
foreach (TreeNode node in TreeViewArticleCatagory.Nodes)
{
CheckNodes(node);
}

//foreach (TreeNode node in TreeViewArticleCatagory.Nodes)
//{
// foreach (DataRow dr in dt.Rows)
// {
// if (Convert.ToString(dr["Item"]) == node.Value)
// {
// node.Checked = true;
// //TreeViewArticleCatagory.SelectedNode.Checked = true;
// }
// }
//}
}
}
private void CheckNodes(TreeNode _node)
{
if (_node.ChildNodes.Count > 0)
{
CheckIfNodeValueExistsInDataTable(_node, ds1);

foreach (TreeNode _childNode in _node.ChildNodes)
{
CheckNodes(_childNode);
}
}
else
{
CheckIfNodeValueExistsInDataTable(_node, ds1);
}
}
private void CheckIfNodeValueExistsInDataTable(TreeNode _node, DataSet _dsCatogories)
{
DataTable dtNodes = _dsCatogories.Tables[0];
DataRow[] drow= dtNodes.Select("Item='" + _node.Value+"'");
if (drow != null && drow.Length > 0)
{
_node.Checked = true;
}
}
Thank you very much for your help...
Your hints help a lot..
Thanks once again..

1 solution

Try looking at node.Nodes - it is a list of child nodes for this node.
 
Share this answer
 
Comments
arindamrudra 25-Aug-10 5:01am    
It is not working...
I tried..
arindamrudra 25-Aug-10 6:45am    
Reason for my vote of 3
The hints helped a lot

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