2012-07-04 57 views
0

我有兩個名爲ProductListItemList的ArrayList。將ArrayList中的值設置爲TreeView

ProductList hold two string []數組。

string[0] string[1] 
--------- ----------- 
ProductID ProductName 
--------- ------------- 
    A001  Food 
    B120  NotFood 

ITEMLIST容納三串[]陣列。

string[0] string[1] string[2] 
--------- ----------- ----------- 
ProductID ItemID  ItemName 
--------- ----------- ----------- 
    A001  X12332  Rice 
    A001  X2133   Pepsi 
    A001  X12450  Sardine 
    B120  H1LKL   Pen 
    B120  JLA122  Printer 

我想在TreeView中顯示這些數據。

所以我寫的代碼如下所示:

for(int i = 0; i <ProductList.Count; i++) //loop for every item in ProductList 
{ 
    TreeNode node = new TreeNode(((string[])ProductList[i])[1]); //create Parent node using ProductName 
    TreeView.Nodes.Add(node); //add node into TreeView 
    for(int j = 0; j < ItemList.Count; i++) //loop for every item in ItemList 
    { 
     if(((string[])ProductList[i])[0] == ((string[])ItemList[j])[0]) //Compare if ProdutID in ProductList same with ProductID in ItemList 
     { 
      node.Nodes.Add(((string[])ItemList[j])[2]); //Add ItemName from ItemList as Child node for current Parent Node 
     } 
    } 
} 

運行上面的代碼中,我得到以下結果:

+Food 
- Rice 
- Pepsi 
- Sardine 
+NotFood 
- Pen 
- Printer 

問題: 我怎樣才能獲得ItemID時用戶選擇節點?

謝謝。

+0

抱歉沒有回答您的問題,但有一點重要提示:您應該將您的ArrayList更改爲列表。這樣你就不需要那些裝箱((string [])item)。你的代碼將變得更乾淨更快。 –

+0

@AndreCalil即時通訊使用.net 2.0。不支持List屬性。 btw.thx。 –

+0

請參閱MSDN上的列表文檔:http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.80).aspx –

回答

0

現在,您的問題點:TreeNode有兩個有趣的屬性,名稱和文本。文本是用戶閱讀的內容,Name是一個任意的,名字。也許,當您將項目添加到TreeView時,您可以將ItemID設置爲TreeNode的名稱。

0

您可以使用Tag屬性來存儲有關該節點的其他信息(在您的情況下可以容納ID)。稍後在節點上單擊,您應該可以獲取此信息。這裏是MSDN Link