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);
}
}
}