I need to represent the following hierarchy in a tree control:
10001
|
+--10001-01
| |
| +-- A
| +-- B
+--10001-02
+-- A
+-- C
Here is my code, which gives me the dup key error on adding the 10001-02->A node:
UltraTreeNode node = null, childNode = null;
foreach (DataRow row in dt.Rows)
{
curAccount = (string)row["AccountCode"];
curSubAccount = (string)row["SubAccountCode"];
curFund = (string)row["FundShortName"];
int accountId = (int)row["AccountId"];
int subAccountId = (int)row["SubAccountId"];
int fundId = (int)row["FundId"];
if (curAccount != prevAccount)
Trace.WriteLine("Added node " + curAccount);
node = tvInvestor.Nodes.Add(curAccount);
}
else
node = tvInvestor.Nodes[curAccount];
if (curSubAccount != prevSubAccount)
Trace.WriteLine("Added child node " + curSubAccount);
childNode = node.Nodes.Add(curSubAccount);
Trace.WriteLine("Child node children count = " + childNode.Nodes.Count);
childNode = node.Nodes[curSubAccount];
Trace.WriteLine("Adding grandchild node " + curFund);
childNode.Nodes.Add(curFund); -- > Error on this line
prevAccount = (string)row["AccountCode"];
prevSubAccount = (string)row["SubAccountCode"];
What am I doing wrong?
Thanks Mike for the update.
That is correct. UltraTree does not allow this.
Niodes in the tree are not required to have a key. But if they do have a key, it must be unique to the entire tree, not just the collection it's in. One advantage to this approach is that the UltraTree control has a FindNodeByKey method which can find a node anywhere in the tree.
Mike,
TreeView allows to have same child for different parent node.
Below code is possible through TreeView, whereas in UltraTree it fails.
// Add a root node TreeNode node = this.treeView1.Nodes.Add("Node 1");
// Add some child nodes TreeNode childNode = node.Nodes.Add("child node 1"); childNode.Nodes.Add("child child 1"); childNode.Nodes.Add("child child 2"); childNode.Nodes.Add("child child 3");
// Add some grandchild nodes childNode = node.Nodes.Add("child node 2"); childNode = node.Nodes.Add("child node 3"); childNode = node.Nodes.Add("child node 4");
TreeNode node2 = this.treeView1.Nodes.Add("Node 2"); node2.Nodes.Add("child node 1");
Thanks for the answer. That was exactly it. I've set the key to be the full node path and it works beautifully.
Chandra
You are trying to add two nodes to the tree with the same key: "A".
All nodes in the tree must have unique keys across the entire tree control, not just the nodes collection the node is in.
So your "A" node will either have to have a unique key - maybe by combining the keys of it's ancestors, or else you can add the nodes and set their Text without specifying any key.