Hierarchal data and tree views


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'm not sure if this is the best place for this question or in the BusinessObjects section...



I have several hierarchal data structures and would like to present them in a tree view. Some of these structures are from within a single table and some span multiple tables. I looking for any suggestions, guidelines on the best way to do this. I'm assuming that in any case I'm likely going to be getting a DataTable from a BO and manually building the tree, but maybe there is a better way.



So, the two types of hierarchies are:



1. parent child within a single table

2. parent child within a single table and with children within another table



So, how best to implement this sort of thing?
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
TreeViews are a wonderful tool in .NET and we use them all over the place...however, though there are similarities between each use, the TreeView is always a little bit different, thus making it a little more difficult to have a hard and fast standard.

Here are some of the standards that we follow though.  The best advice is to create recursive methods that build each of the nodes.

Private Sub LoadTree()
    '-- Establish Locals
    Dim loNode As TreeNode

    '-- Clear the tree
    MyTreeView.Nodes.Clear()

    '-- Cycle through all of the parent records
    If MyBO.MoveFirst()
         Do
              '-- Create the new root node
              loNode = New TreeNode(MyBO.MyTitleField)

              '-- Load any children nodes
              LoadChildNodes(MyBO, loNode)
         Loop While MyBO.MoveNext()
    End If
End SUb

Private Sub LoadChildNodes(Byval ParentBO As BusinessLayer, Byval ParentNode As TreeNode)
    '-- Establish Locals
    Dim loNode As TreeNode
    Dim loChildBO As New MyChildBO

    '-- Load the child records
    loChildBO.FillByParentPrimaryKey(ParentBO.PrimaryKey)

    '-- Cycle through the child nodes
    If loChildBO.MoveFirst()
        Do
            '-- Create the node
            loNode = New TreeNode(loChildBO.TitleField)

            '-- Add to parent nodes
            ParentNode.Nodes.Add(loNode)

            '-- Check for child nodes
            If loChildBO.HasChildRecords Then
                  LoadChildNodes(loChildBO, loNode)
            End If
        Loop While loChildBO.MoveNext()
    End If

    '-- Clean Up
    loChildBO.Dispose()
End Sub

Also, we generally create a class and store it in the tag property of each node so that when it is clicked we know exactly what to do.  You can see a simple example of this in the samples that were installed with the framework.  I think it is called Explorer Form Sample.

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search