{"TestAlarm1.T1", "Test1.S1.S4", "TestAlarm1.T2", "test2.T1", "Test1.S1.S2.Tag1"}
Needs to be built as
"TestAlarm1"
"T1"
"T2"
"Test1"
"S1"
"S4"
"S2"
"Tag1"
"test2"
"T1"
So Split the string and look at the first element. Does it exist in the tree Nodes list?
If not, create it.
If so, use that.
Then look at the second element: does it exist in the first node Nodes list?
If not, create it.
If so, use that.
And so on.
At the moment, you only change nodes when you are at the "root element" - but your data doesn't reflect that, you have three levels of data.
And you don't check to see if a node is already there at all!
Quote:
the string added is just for example, data can have any number of sub parent,child. i need the actual logic to compare the current holding string value with all nodes in tree view. i am stuck at this place only
And that is what I gave you!
It's pretty simple:
string[] tagdata = { "TestAlarm1.T1", "Test1.S1.S4", "TestAlarm1.T2", "test2.T1", "Test1.S1.S2.Tag1" };
myTreeView.Nodes.Clear();
TreeNode node;
foreach (string tag in tagdata)
{
string[] data = tag.Split('.');
TreeNodeCollection nodes = myTreeView.Nodes;
foreach (string name in data)
{
if (nodes.ContainsKey(name))
{
node = nodes[name];
}
else
{
node = new TreeNode(name);
node.Name = name;
nodes.Add(node);
}
nodes = node.Nodes;
}
}
Only took 3 minutes to design, write, and test...