PM,
Honestly, I don't know, since you weren't too clear about the format of
^your^ data and how you wanted ^your^ tree to behave.
With what you said, I would adjust the code like this:
void AddBranch(TreeNode root, DataColumn[] columns, DataRow data)
{
// Cycle through the columns. The columns are assumed
// to be the columns that represent the order of the heiarchy, so that
// columns[0] is the first node attached to root, columns[1] is
// a node attached to the node created by columns[0] and so on.
foreach (DataColumn column in columns)
{
// Get the data for the column.
string val = data[column].ToString();
// The node.
TreeNode foundNode = null;
// See if it exists in the collection. If it does, then
// set the item to that. Otherwise, create a new one.
foreach (TreeNode node in root)
{
// If the text on the node is the same
// as your value, then use that.
if (node.Text == val)
{
// Set the found node to the node.
foundNode = node;
}
}
// If a node was found, then set the root to that node. Otherwise
// add the node to the root.
if (foundNode != null)
{
// Set the root to that node.
root = foundNode;
}
else
{
// Add the node.
root = root.Nodes.Add(val);
}
}
}
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
<mi************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Thanks again. But in this case won;t the number of nodes in level1 be
equal to number of rows in the data table. Suppose I have three rows in
my data table.
country state city
USA NJ jersey city
USA FL tempa
USA VT richmond
Then in the tree I will have three nodes at level 1 namely
USA
USA
USA
Shouldn't there be a single node named USA in this case.
Thanks,
PM