Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I want to fill a treeview with rows of a datatable that have already been stored in a database. I used a code like this:(Obj is the returned row)
(the root node have been specified before and I want to put just the child nodes)
C#
foreach (TreeNode tn in treeView1.Nodes)
{
    if (tn.Text == Obj[8].ToString())
    {
        TreeNode NewNode = new TreeNode();
        NewNode.Name = Obj[0].ToString();
        NewNode.Text = Obj[10].ToString();
        tn.Nodes.Add(NewNode);
        treeView1.SelectedNode = NewNode;
        treeView1.SelectedNode.Expand();
    }
}

But only one child is added to the treeview and after that it seems that the number of nodes of the tree do not change (update) and still remains 1. How should I update the treeview after a node added to it?
Thanks so Much In Advance!
Good Luck!
Posted

Have you set breakpoints to see that this code is called more than once based on your data source ? As it stands, if the number of nodes was one to start with, iterating over the number of nodes can only add one, you only go through your loop once.
 
Share this answer
 
The loop above is within another loop that is:
C#
for (int i = 0; i < NoOfRows; i++)
 {
     Obj = myDataSet.Tables["myTable"].Rows[i].ItemArray;
}


that
int NoOfRows = myDataSet.Tables["myTable"].Rows.Count;


I first put the first node (root node in the tree):

for (int i = 0; i < NoOfRows; i++)
{
    Obj = myDataSet.Tables["myTable"].Rows[i].ItemArray;
    if (Obj[8].ToString()=="")
    {
        TreeNode RootNode = new TreeNode();
        RootNode.Name = Obj[0].ToString();
        RootNode.Text = Obj[10].ToString();
        treeView1.Nodes.Add(RootNode);
    }
}

and as I said before, each time one node is added to the treeview.

But the treeview.nodes.count after adding another node does not changes. I think that I should update it somehow in order to get the newly added node.
thanks for your fast reply!
 
Share this answer
 
I think that it is because the new node is added to a node not treeview and therefore it is not included in the number of nodes!
I tried insert instead of add function, but it has its own problems!
No other answer? Please Help!:confused:
Thank you a lot!
 
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