Try this:
NDA.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string tagName = Convert.ToString(row["tag_name"]);
string[] parts = tagName.Split('.');
TreeNodeCollection nodes = TreeView1.Nodes;
for (int i = 0; i < parts.Length; i++)
{
string valuePath = string.Join(TreeView1.PathSeparator, parts, 0, i);
TreeNode node = TreeView1.FindNode(valuePath);
if (node == null)
{
node = new TreeNode(parts[i]);
nodes.Add(node);
}
nodes = node.ChildNodes;
}
}
The FindNode
method[
^] expects the full path of the node to find, whereas you're just passing the value of the current node.
So, for example, instead of looking for a node with the path
MainNode1/Parent1
, you're looking for a node with the path
Parent1
. Since you never add
Parent1
to the root of the tree, that node never exists.
The value path contains a delimiter-separated list of node values that form a path from the root node to the current node.