我有兩個名爲ProductList
和ItemList
的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
時用戶選擇節點?
謝謝。
抱歉沒有回答您的問題,但有一點重要提示:您應該將您的ArrayList更改爲列表。這樣你就不需要那些裝箱((string [])item)。你的代碼將變得更乾淨更快。 –
@AndreCalil即時通訊使用.net 2.0。不支持List屬性。 btw.thx。 –
請參閱MSDN上的列表文檔:http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.80).aspx –