Results 1 to 14 of 14

Thread: VB2010 - Auto populate TreeView

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Question VB2010 - Auto populate TreeView

    Hi there,

    Please bear with me as I am not familiar yet with all aspects and possibilities of Visual Basic.

    I have a Microsoft SQL Express 2008 database setup with values shown in the attached image "Image1.jpg".

    This is just an example, there can be more or less disciplines, the categories arn't necessarily the same for each discipline and each category has a set of sub-categories.

    Upon form load I am trying to get this information to automatically populate a treeview, as shown in the attached image "Image2.jpg".

    I have been trying for a couple of days now, and not having much luck. The code example I found was on youtube, but unfortunately it uses a binded access database and a dataset, the code is as follows:

    Code:
    Imports System.Data.OleDb
    
    
    Public Class Form1
    
        Private Sub TreeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Me.Validate()
            Me.TreeBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.TestDataSet)
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'TestDataSet.tree' table. You can move, or remove it, as needed.
            Me.TreeTableAdapter.Fill(Me.TestDataSet.tree)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TreeTableAdapter.Connection.Open()
            TreeView1.Nodes.Clear()
            FillTree("a", "first", Nothing)
            TreeTableAdapter.Connection.Close()
        End Sub
    
        Public Sub FillTree(ByVal Key As String, ByVal Txt As String, ByVal N As TreeNode)
            Dim cn As OleDbConnection
            Dim cmd As OleDbCommand
            Dim NN As TreeNode
            If N Is Nothing Then
                NN = TreeView1.Nodes.Add(Key, Txt)
            Else
                NN = N.Nodes.Add(Key, Txt)
            End If
            cn = TreeTableAdapter.Connection
            cmd = New OleDbCommand("select * from tree where node_parent='" & Key & "'", cn)
            Dim dr = cmd.ExecuteReader
            Do While dr.Read()
                FillTree(dr("node"), dr("node_name"), NN)
            Loop
    
    
            dr.Close()
            cmd.Dispose()
        End Sub
    
    End Class
    Also this seemed to only ever add 1 root node "FillTree("a", "first", Nothing)".

    I had also tried this example http://support.microsoft.com/kb/320755.
    I downloaded the test database, I got no errors but the TreeView was empty.

    I had managed to use "SELECT DISTINCT Discipline FROM Table1" sql statement, and use a while loop with TreeView1.Nodes.Add(retrieved data) to populate the root nodes. My only idea now is to keep running sql statements e.g. SELECT Category FROM Table1 WHERE Discipline ='" & TreeView1.Nodes(i).Text & "'" and so on for the child and grandchild nodes. But I'm not even sure if this will work and I am hoping someone knows of a neater easier way.

    I am getting quite frustrated with this, so if anyone can help or even post some code examples it would be greatly appreciated.

    Thanks in advance, and I await your replies.


    squatman
    Attached Images Attached Images   

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width