Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am currently holding data in string array with '.' seperation. i need to plot the same data in tree view.
eg.
string[] tagdata = {"TestAlarm1.T1", "Test1.S1.S4", "TestAlarm1.T2", "test2.T1", "Test1.S1.S2.Tag1"};


What I have tried:

i have wriiten following code, but i am getting multiple parent node of same name which is wrong.
private void Form1_Load(object sender, EventArgs e)
        {
            TreeNode node = null;

            string[] tagdata = {"TestAlarm1.T1", "Test1.S1.S4", "TestAlarm1.T2", "test2.T1", "Test1.S1.S2.Tag1"};

            for (int i = 0; i < tagdata.Length; i++)
            {
                string[] data = tagdata[i].Split('.');

                for (int j = 0; j < data.Length; j++)
                {
                    if (j == 0)
                    {
                        node = treeView1.Nodes.Add(data[j]);
                    }
                    else
                    {
                        node.Nodes.Add(data[j]);
 
                    }
                }
            }
}
Posted
Updated 20-Feb-19 0:36am
Comments
Richard MacCutchan 20-Feb-19 6:31am    
You need to check first if a node name already exists in the tree, before you add it.
Member 12694392 20-Feb-19 6:37am    
that is what i am trying to figure out how it can be done. please help

1 solution

{"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...
 
Share this answer
 
v2
Comments
Member 12694392 20-Feb-19 6:44am    
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
OriginalGriff 20-Feb-19 6:56am    
Answer updated.
Member 12694392 20-Feb-19 7:10am    
it worked thanks
onlyastranger 26-Jul-21 11:35am    
I've just come across your solution. And you've made my day, OriginalGriff. Thanks a lot...
OriginalGriff 26-Jul-21 11:44am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900