如何从treeview

时间:2016-08-22 09:52:57

标签: c# treeview

我使用以下代码填充了树视图:

        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中操作,重命名和删除其中一些元素后,是否可以使用新值重建我的Grapics结构?

构建此结构使每个Graphic都有许多模板,每个模板都有许多别名,此代码定义如下:

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

如果这不容易实现,从不同角度处理这个问题的最佳方法是什么?

提前致谢!

1 个答案:

答案 0 :(得分:-1)

试试这个,

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