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
710
Databinding Self-Referencing Data to a WinTree
posted

I have a table which defines a  hierarchy which is self-referencing.

I retrieve the data into a DataSet DataTable, and create a DataRelation for the self-referencing column (i.e. MyID and MyParentID). When I data bind the table to the tree, I see every node in the tree at the first level.  Only the TOP node should be at the first level, followed recursively by the children at each level.

What am I missing?

The TOP node in my hierarchy is indicated by a NULL parent ID.  

Parents
No Data
Reply
  • 469350
    Verified Answer
    Offline posted

    Hi,

    You're not missing anything. If you set up a DataSet with a single table and a single relationship which relates the table back to itself, the root-level table is still going to have all of the rows of data. So the tree is just displaying what the DataSet gives it.

    You have a couple of options here.

    The easiest thing to do would probably be to handle the InitializeDataNode event of the tree. You could check the key value and the ParentNode of each node initialize. If the ParentNode is null, you know the node is at the root level. And then if the ParentID is not null, you know the node should not be visible and you can set the Hidden property on the node to true.

    Another option would be to do this on the DataSet. This is a little more complicated coding-wise, but it's probably a bit more efficient. What you would do is create two tables. The first table would be created from a query that only returns rows where the ParentID is null. Then you create a relation from that table to the main table (which includes everything). And then a second relation from teh main table back to itself. This way the data would accurately reflect the structure you want.

    The only tricky part about this is that the tree will not have 2 different ColmunSets by default. So you would probably want to turn off AutoGenerateColumnSets and generate one ColumnSet yourself and use it as the RootColumnSet in the tree.

Children