Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
520
Key already exists error in UltraWinTree
posted

 

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);

                }

                else

                    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?

 

 

Parents
  • 469350
    Verified Answer
    Offline posted

    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.

Reply Children