如何从树视图生成层次结构

本文关键字:层次结构 视图 | 更新日期: 2023-09-27 18:03:35

我已经用下面的代码填充了一个树视图:

        private void updateTree()
    {
        treeView1.Nodes.Clear();
        List<TreeNode> graphicsNodes = new List<TreeNode>();
        foreach (Graphic graphic in graphics)
        {
            List<TreeNode> templateNodes = new List<TreeNode>();
            foreach (Template template in graphic.templates)
            {
                List<TreeNode> aliasNodes = new List<TreeNode>();
                foreach(Alias alias in template.aliases)
                {
                    aliasNodes.Add(new TreeNode(alias.aliasName + ": " + alias.aliasValue));
                }
                templateNodes.Add(new TreeNode(template.templateName, aliasNodes.ToArray()));
            }
            graphicsNodes.Add(new TreeNode(graphic.fileName.Replace(".g", ""), templateNodes.ToArray()));
        }
        treeView1.Nodes.Add(new TreeNode("Graphics", graphicsNodes.ToArray()));
    }

在操纵、重命名和删除TreeView中的一些元素后,是否有可能用新值重建我的图形结构?

这个结构是这样构建的,每个图形有许多模板,每个模板有许多别名,这段代码定义如下:

public class Graphic
{
    public string filePath;
    public string fileName;
    public List<Template> templates;
    public Graphic(List<Template> _templates, string fpath, string fName)
    {
        templates = _templates;
        fileName = fName;
        filePath = fpath;
    }
}
public class Template
{
    public List<Alias> aliases;
    public string templateName;
    public Template(string instString, string userdataString)
    {
        templateName = (instString.Replace("inst ", "").Replace(" 0 0", "")).Replace(" ","");
        aliases = new List<Alias>();
        string[] aliasStrings = userdataString.Replace("'"","").Split(new string[1] { "text_alias" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string segment in aliasStrings)
        {
            if (segment.Contains("userdata")) { continue; }
            string aliasString = "text_alias" + segment;
            string[] sections = aliasString.Split('^');
            aliases.Add(new Alias(sections[0].Replace("text_alias=", ""), (sections[1].Replace("text_exp_flag=", "") == "1"), sections[2].Replace("alias_new_name=", ""), sections[3].Replace("alias_value=", ""))); //Sorry!
        }
    }
}
public class Alias
{
    public string aliasName;
    public string aliasValue;
    public string newName;
    public bool expectedFlag;
    public Alias(string name, bool expected, string nName, string value)
    {
        aliasName = name;
        aliasValue = value;
        newName = nName;
        expectedFlag = expected;
    }
}

如果这是不容易实现的,最好的方法是从不同的角度来解决这个问题?

提前感谢!

如何从树视图生成层次结构

试试这个,

for (int i = 0; i <= dst.Tables[0].Rows.Count - 1; i++)
            {
                nodeTables.Nodes.Add(dst.Tables[0].Rows[i]["SchemaName"].ToString() + "." + dst.Tables[0].Rows[i]["TableName"].ToString());
                nodeTables.Nodes[i].ImageIndex = 2;
                nodeTables.Nodes[i].Nodes.Add("Columns");
                //Filling the Column Names
                DataSet dstCol = new DataSet();
                dstCol = common.GetColumns(Convert.ToInt32(dst.Tables[0].Rows[i]["object_Id"]));
                for (int col = 0; col <= dstCol.Tables[0].Rows.Count - 1; col++)
                {
                    nodeTables.Nodes[i].Nodes[0].ImageIndex = 0;
                    nodeTables.Nodes[i].Nodes[0].Nodes.Add(dstCol.Tables[0].Rows[col]["name"].ToString());
                    nodeTables.Nodes[i].Nodes[0].Nodes[col].ImageIndex = 1;
                }
            }