【问题标题】:How I can fill a treeView with a List<string>? [closed]如何用 List<string> 填充 treeView? [关闭]
【发布时间】:2013-05-17 04:05:08
【问题描述】:

您好,我有一个关于 treeview 的问题。

我有一个 List 列表和名为 treeLic 的树视图。现在我想用这个列表填充这个树视图。我可以这样做吗?怎么做?

XmlReader xr = XmlReader.Create(new StringReader(final_output));

           while (xr.Read())
           {
               switch (xr.Name)
               {
                   case "FEATURE":
                       if (xr.HasAttributes)
                       {
                           while (xr.MoveToNextAttribute())
                           {
                               if (xr.Name == "NAME")
                               {
                                   liste.Add(xr.Value);
                               }
                           }
                       }
                       break;

               }
           }

treeLic. ????? //fill this treeview with this list liste

【问题讨论】:

标签: c# string list collections treeview


【解决方案1】:

试试这个。

XmlReader xr = XmlReader.Create(new StringReader(final_output));

       while (xr.Read())
       {
           switch (xr.Name)
           {
               case "FEATURE":
                    TreeNode root = MyTreeView.Nodes.Add("FEATURE");
                   if (xr.HasAttributes)
                   {
                       while (xr.MoveToNextAttribute())
                       {
                           if (xr.Name == "NAME")
                           {
                               TreeNode workingNode = root.Nodes.Add(xr.Value.ToString());
                               liste.Add(xr.Value);
                           }
                       }
                   }
                   break;

           }
       }

【讨论】:

    【解决方案2】:
    var nodes = XDocument.Load(fileName)
                .Descendants("FEATURE")
                .Select(f => new TreeNode((string)f.Attribute("NAME")))
                .ToArray();
    
    treeLic.Nodes.AddRange(nodes);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多