如何使用树视图添加多级节点? [英] How do I add multi level nodes using tree view?

查看:86
本文介绍了如何使用树视图添加多级节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用'。'分隔在字符串数组中保存数据。我需要在树状视图中绘制相同的数据。

例如。

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





我的尝试:



i已经跟随代码,但我得到多个同名的父节点,这是错误的。



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

推荐答案

{"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:

添加的字符串只是例如,数据可以有任意数量的sub parent,child。我需要实际逻辑来比较当前保持字符串值与树视图中的所有节点。我只是被困在这个地方

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

设计,编写和测试只花了3分钟......

Only took 3 minutes to design, write, and test...


这篇关于如何使用树视图添加多级节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆