2009-10-27 64 views
0

我有這樣的轉移的StringList成樹狀

「根」

「根/ Item1的」

「根/項目2」

「根/項目3 /項的字符串列表的SubItem1"

「根/項目3/SubItem2」

「根/項目4 /的SubItem1」

「AnotherRoot」

如何將此字符串列表轉換爲樹視圖?

回答

2

您可以將每個項目拆分爲它的子串。然後通過遞歸查找每個項目,如果父項存在添加到它,並且父項不存在創建它。

如果你不知道怎樣做,我心底張貼您的樣本代碼

示例代碼

public void AddItem(TreeView treeControl, TreeNode parent, string item) 
    { 
     TreeNodeCollection nodesRef = (parent != null) ? parent.Nodes : treeControl.Nodes; 
     string currentNodeName; 
     if (-1 == item.IndexOf('/')) currentNodeName = item; 
     else currentNodeName = item.Substring(0, item.IndexOf('/')); 
     if (nodesRef.ContainsKey(currentNodeName)) 
     { 
      AddItem(treeControl, nodesRef[currentNodeName], item.Substring(currentNodeName.Length+1)); 
     } 
     else 
     { 
      TreeNode newItem = nodesRef.Add(currentNodeName, currentNodeName); 
      if (item.Length > currentNodeName.Length) 
      { 
       AddItem(treeControl, newItem, item.Substring(item.IndexOf('/', currentNodeName.Length) + 1)); 
      } 
     } 
    } 

和呼叫者例如:

 string[] stringArr = { 
           "Root", 
           "Root/Item1", 
           "Root/Item2", 
           "Root/Item3/SubItem1", 
           "Root/Item3/SubItem2", 
           "Root/Item4/SubItem1", 
           "AnotherRoot" 
          }; 
     foreach (string item in stringArr) 
     { 
      AddItem(treeView1, null, item); 
     } 
+0

示例代碼將非常讚賞 – tomfox66 2009-10-27 22:42:26

0

一種方法是迭代項目拆分項目,並將它們推到列表上,如果父項不匹配,則從列表中彈出項目,直到堆棧爲空或您有墊子CH。

0

您可以使用此代碼:

private void button1_Click(object sender, EventArgs e) { 
     List<String> paths = new List<String> { 
      "Root", "Root/Item1", "Root/Item2", "Root/Item3/SubItem1", 
      "Root/Item3/SubItem2", "Root/Item4/SubItem1", "AnotherRoot" 
     }; 

     List<TreeNode> nodeCollection = new List<TreeNode>(); 

     foreach (var path in paths) { 
      AddPath(nodeCollection, path); 
     } 

     treeView1.Nodes.Clear(); 
     treeView1.Nodes.AddRange(nodeCollection.ToArray()); 
    } 

    public void AddPath(List<TreeNode> collection, String path) { 
     LinkedList<String> pathToBeAdded = new LinkedList<String>(path.Split(new String[] { @"/" }, StringSplitOptions.RemoveEmptyEntries)); 

     if (pathToBeAdded.Count == 0) { 
      return; 
     } 

     String rootPath = pathToBeAdded.First.Value; 
     TreeNode root = collection.FirstOrDefault(n => n.Text.Equals(rootPath)); 

     if (root == null) { 
      root = new TreeNode(rootPath); 
      collection.Add(root); 
     } 

     pathToBeAdded.RemoveFirst(); 
     AddPath(root, pathToBeAdded); 
    } 

    public void AddPath(TreeNode rootNode, LinkedList<String> pathToBeAdded) { 
     if (pathToBeAdded.Count == 0) { 
      return; 
     } 

     String part = pathToBeAdded.First.Value; 
     TreeNode subNode = null; 

     if (!rootNode.Nodes.ContainsKey(part)) { 
      subNode = rootNode.Nodes.Add(part, part); 
     } else { 
      subNode = rootNode.Nodes[part]; 
     } 

     pathToBeAdded.RemoveFirst(); 
     AddPath(subNode, pathToBeAdded); 
    } 

希望這有助於。

Ricardo Lacerda Castelo Branco