TreeView + DataBinding

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

I would like to bind my access database to a Treeview control, but i don't know how to do this?

I would like it so that the nodes are the "Broad" field values and the the childs of those nodes are the "Specific" fields (see data structure diagram below)

be8a752ba9_dfsdspng.png


How can this be done?

Thanks.

EDIT:

Appologies if this should be in a different forum, i wasnt sure if this should go here or data access? maybe somewhere else? move if applicable.

Thanks, help much apprechiated.
 
You can not databind the treevew, to fill the treevew at runtime you must iterate an object and build the nodes record at a time

off the top of my head this should work

VB.NET:
Private Sub LoadTree()
Dim rootnode As New TreeNode
rootnode.Text = "My Top Level Node"
rootnode.Tag = 0
Me.treeview1.nodes.add(rootnode)
RenderTree(rootnode, 0)
End Sub
 
Private Sub RenderTree(ByVal intreenode As TreeNode, ByVal ParentID As Integer)
'Execute sqlcommand and load records to SomeCollectionClass were id = intreenode.tag
'SomeCollectionClass will need the properties
'DisplayName String
'ID Integer
'ParentID Integer
Dim MyCollection As SomeCollectionClass() = HowIGetMyRecordFuntion(ParentID)
For Each record As SomeCollectionClass In MyCollection
Dim childNode As New TreeNode
childNode.Text = record.DisplayName
childNode.Tag = record.ID
intreenode.Nodes.Add(childNode)
RenderTree(childNode, record.ParentID)
Next
End Sub
this method will render the complete tree - it works fine for small records, but when things get large, your app will hang while iterating, so what you would do is

VB.NET:
Private Sub LoadTree()
Dim rootnode As New TreeNode
rootnode.Text = "My Top Level Node"
rootnode.Tag = 0
Me.treeview1.nodes.add(rootnode)
RenderTree(rootnode, 0)
End Sub
 
Private Sub RenderTree(ByVal intreenode As TreeNode, ByVal ParentID As Integer)
'Execute sqlcommand and load records to SomeCollectionClass were id = intreenode.tag
'SomeCollectionClass will need the properties
'DisplayName String
'ID Integer
'ParentID Integer
Dim MyCollection As SomeCollectionClass() = HowIGetMyRecordFuntion(ParentID)
For Each record As SomeCollectionClass In MyCollection
Dim childNode As New TreeNode
childNode.Text = record.DisplayName
childNode.Tag = record.ID
intreenode.Nodes.Add(childNode)
Next
End Sub
 
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
RenderTree(e.Node, e.Node.Tag)
End Sub
this will speed you initial tree load but slow down navigation of the tree

oh but in the treeview1_afterSelect event you have to check to ensure that the node has not already been selected or you are going to get duplicate nodes and more calls to the db then needed

VB.NET:
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.GetNodeCount(True) = 0 Then
RenderTree(e.Node, e.Node.Tag)
End If
End Sub
 
Last edited by a moderator:
Hi there,

Thanks for your reply, I managed to get this working.

Here is what i got, i render the full table straight away. I am yet to be given the full set of data so i am unsure on how fast/slow it is.

VB.NET:
            While dbrsub.Read()

                Dim sub As String = dbrsub(0).ToString()

                tv.Nodes.Add(sub, sub)

                While dbrdsub.Read()

                    tv.Nodes.Item(sub).Nodes.Add(dbrdsub(0).ToString, dbrdsub(1).ToString)

                End While

                dbrdsub.Close()

            End While

            dbrsub.Close()

Something like that anyways.

Thanks again.
 
Back
Top