add a child node , tree view

  • Thread starter Thread starter Dino L.
  • Start date Start date
D

Dino L.

I am putting data from DataTable to treeView

foreach( DataRow aRow in aTable.Rows)
{
TreeNode tnode = new TreeNode(aRow[1].ToString() + aRow[2].ToString() +
" " + aRow[3].ToString());
treeView1.Nodes.Add(tnode);
//till here code works fine
//now I wanna add child nodes for last inserted node
foreach( DataRow GrupaRow in TabelaGrupe.Rows)
{
if(statment)
{
TreeNode tnodegr = new TreeNode(GrupaRow[2].ToString()
+ " " + GrupaRow[3].ToString());
//what should I write here to put node tnodegr as a child to last
inserted tnode
treeView1.Nodes.Add(tnodegr);//????
}
}
treeView1.ExpandAll();
}
 
foreach( DataRow aRow in aTable.Rows)
{
TreeNode tnode = new TreeNode(aRow[1].ToString() + aRow[2].ToString() +
" " + aRow[3].ToString());
//till here code works fine
//now I wanna add child nodes for last inserted node
foreach( DataRow GrupaRow in TabelaGrupe.Rows)
{
if(statment)
{
TreeNode tnodegr = new TreeNode(GrupaRow[2].ToString()
+ " " + GrupaRow[3].ToString());
tnode.Nodes.Add(tnodegr);
treeView1.Nodes.Add(tnode);
}
}
treeView1.ExpandAll();
}
 
A small change

foreach( DataRow aRow in aTable.Rows)
{
TreeNode tnode = new TreeNode(aRow[1].ToString() + aRow[2].ToString() +
" " + aRow[3].ToString());
//till here code works fine
//now I wanna add child nodes for last inserted node
foreach( DataRow GrupaRow in TabelaGrupe.Rows)
{
if(statment)
{
TreeNode tnodegr = new TreeNode(GrupaRow[2].ToString()
+ " " + GrupaRow[3].ToString());
tnode.Nodes.Add(tnodegr);
}
}
treeView1.Nodes.Add(tnode);
}
treeView1.ExpandAll();



Chester Ragel said:
foreach( DataRow aRow in aTable.Rows)
{
TreeNode tnode = new TreeNode(aRow[1].ToString() + aRow[2].ToString() +
" " + aRow[3].ToString());
//till here code works fine
//now I wanna add child nodes for last inserted node
foreach( DataRow GrupaRow in TabelaGrupe.Rows)
{
if(statment)
{
TreeNode tnodegr = new TreeNode(GrupaRow[2].ToString()
+ " " + GrupaRow[3].ToString());
tnode.Nodes.Add(tnodegr);
treeView1.Nodes.Add(tnode);
}
}
treeView1.ExpandAll();
}


Dino L. said:
I am putting data from DataTable to treeView

foreach( DataRow aRow in aTable.Rows)
{
TreeNode tnode = new TreeNode(aRow[1].ToString() + aRow[2].ToString() +
" " + aRow[3].ToString());
treeView1.Nodes.Add(tnode);
//till here code works fine
//now I wanna add child nodes for last inserted node
foreach( DataRow GrupaRow in TabelaGrupe.Rows)
{
if(statment)
{
TreeNode tnodegr = new TreeNode(GrupaRow[2].ToString()
+ " " + GrupaRow[3].ToString());
//what should I write here to put node tnodegr as a child to last
inserted tnode
treeView1.Nodes.Add(tnodegr);//????
}
}
treeView1.ExpandAll();
}
 
You can use recursive search here easily like,

private void SearchNodes(TreeNode node)
{
foreach(TreeNode currentNode in node.Nodes)
{
//Do the search for the current node here
if((currentNode.Nodes!=null) && (currentNode.Nodes.Count>0))
{
SearchNodes(currentNode);
}
}
}

Call this methord from somewhere where you want to do the search and pass
the parent node there..

Chester.
 
thank you
there is your function now (with some changes)

private int SearchNodes(TreeNode node, string tekst)
{
int i = 0;
//added by dino :: string tekst is pattern match
try
{
foreach(TreeNode currentNode in node.Nodes)
{
//Do the search for the current node here
//added by dino :: must be >=0 or it want go trough lowest node level
if((currentNode.Nodes!=null) && (currentNode.Nodes.Count>=0))
{
if(currentNode.Text.StartsWith(tekst))
{
currentNode.Expand();
currentNode.BackColor = Color.Red;
i ++;
}
SearchNodes(currentNode,tekst);
//check :: does node match string???
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top