I have two ArrayLists named ProductList and ItemList.
ProductList holds two string[] arrays.
string[0] string[1]
--------- -----------
ProductID ProductName
--------- -------------
A001 Food
B120 NotFood
ItemList holds three string[] arrays.
string[0] string[1] string[2]
--------- ----------- -----------
ProductID ItemID ItemName
--------- ----------- -----------
A001 X12332 Rice
A001 X2133 Pepsi
A001 X12450 Sardine
B120 H1LKL Pen
B120 JLA122 Printer
I want to show these data in a TreeView.
So I wrote the coding as shown below:
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
}
}
}
Running the above code, I get the following result :
+Food
- Rice
- Pepsi
- Sardine
+NotFood
- Pen
- Printer
Question:
How can I get the ItemID when user select the Node?
Thanks.
Now, to the point of your question: TreeNode has two interesting properties, Name and Text. Text is what the user reads, and Name is an arbitrary, well, name. Maybe, when you are adding the items to the TreeView, you could set the ItemID as the Name of the TreeNode.