Binding to hierarchical data

Posts   
 
    
rboarman
User
Posts: 83
Joined: 01-Feb-2005
# Posted on: 21-May-2007 05:18:16   

Hello,

I am finding myself a bit confused as to the best way to bind a control (DataList) to data pulled from a table that has a built-in hierarchy via a self-referencing foreign key relationship. I am using 2.0 adapter model.

The table as a foreign key pointing from ParentGUID to the RowGUID PK of the table.

Table structure:


CREATE TABLE [dbo].[Communities](
    [RowGUID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [ParentGUID] [uniqueidentifier] NULL,
    CONSTRAINT [PK_Communities] PRIMARY KEY CLUSTERED 
(
    [RowGUID] ASC
)) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Communities]  WITH CHECK ADD  CONSTRAINT [FK_Communities_Communities] 
FOREIGN KEY([ParentGUID]) REFERENCES [dbo].[Communities] ([RowGUID])

What’s the best way to bind to this data so that the hierarchical relationship is displayed like this:

Parent 1 - Child 1.1 - Child 1.2 Parent 2 - Child 2.1 - Child 2.2

This is the best article that I could find on the subject:

http://msdn2.microsoft.com/en-us/library/aa478959.aspx

In short, it says that I need to create relationships between the tables loaded in a DataSet then use nested DataBinders.

There must be a better way with LLBLGen.

In my case, the data is slowly changing and read only. Using a typed list seems like the best approach.

Am I heading down the right route? What’s the best way to accomplish this?

Thank you,

Rick

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-May-2007 09:36:11   

There were a lot of discussions about fetching and binding a Self Join Hierarchial data. And whether you want to bind it to a Tree (the common way) or a dataList, won't make much of a difference.

Please check the following links (happy reading): http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8706 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=4757 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=5493 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7330 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=5880 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7862

If it's 2 level deep, you can also try the following approach: - Fetch the entire table in an entityCollection. - Use an EntityView filtered to ParentGUID = null, this should represnt root entities. - Loop on that Parents EntityView and for each item in the EntityView do the following - - Add a node to a tree or an item to a dataList. - - Create another EntityView filtered on ParentGUID = Parent.RowGUID - - Add a child node to a tree or an item to a dataList.