Results 1 to 4 of 4

Thread: Populating treeview from database/structure

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    6

    Populating treeview from database/structure

    Hi, I am new to these forums, first time post. I usually struggle through a problem until I figure out the solution, but I am stumped.

    I have a table in an access database with the following fields.

    ID:
    ParentID:
    Name:


    So, I might have the following data:
    ID|ParentID|Name
    1, 1, Parent1
    2, 1, Child1
    3, 3, Parent2
    4, 2, Grandchild1

    As you can probably guess, ID is a unique autoincrementing number, while parent ID is the ID of the record that is the parent of the current record.

    In the treeview, I want to be able to take that info, and generate the treeview as such.
    A treeview from the above sample table would like simliar to the following:

    - Parent1
    - Child1
    - GrandChild1
    - Parent2

    The reason why I titled this post database/structure is because each seperate record in the database is loaded into a new instance of a class which contains properties for each of the fields (name, id, parentid), and each of those instances of the class are loaded into the hashtable, the key being the ID, and the value being the instance of the class. (may just change it to a structure later, but for now a simple class meets my needs, and is upgradeable in the future to hold some subs or functions if needed).

    It's easy to populate the tree with the Parents, I just check to see if ID = ParentID, after that I need to check the ParentID's to the tag value of each node in the tree (which I made = to the ID of the record), and if so, then add a node under that node. I have came very close, and actually got it all working as long as there are no grandchildren or deeper (greatgrandchildren?).

    If anyone can give me some tips on how to make this work, I would be greatly appreciative. I have been stuck on this for a couple of weeks and all other programming has halted until I can get passed this hurdle. Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    List below is some sample code which populates a treeview freo a database.

    Since I required more properties than the stnadard TreeNode, I created a class which inherited the standard TreeNode and added my own properties.

    New TreeNode Class
    ___________________

    Public Class ViewNode
    Inherits TreeNode

    Public Name As String
    Public Proprty As Integer
    Public Unit As Integer


    Sub New(ByVal Title As String, ByVal Proprty1 As Integer, ByVal Unit1 As Integer)
    MyBase.New()
    Name = Title
    Proprty = Proprty1
    Unit = Unit1
    Me.Text = Name
    End Sub
    End Claass.

    The in the load event of the form:

    Private Sub PropertyAvailable_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim OledbStr As String = "Select * from Localities "
    Dim Cmd1 As OleDbCommand = New OleDbCommand(OledbStr, Cn1)
    Cn1.Open()
    Cn2.Open()
    Cn3.Open()
    Dim dr1 As OleDbDataReader = Cmd1.ExecuteReader()
    TreeView1.BeginUpdate()
    TreeView1.Nodes.Clear()
    Dim AllNode As New ViewNode("All Properties", 0, 0)
    TreeView1.Nodes.Add(AllNode)
    <<< Sets Top level Node >>>
    Dim TreePos As Integer = 1
    While dr1.Read
    Dim LocNode As New ViewNode(dr1("Locality"), 0, 0)
    AllNode.Nodes.Add(LocNode)
    <<< Sets Parent Node >>>
    OledbStr = "Select * from PropAvail where Locality = '" & Trim(dr1("Locality")) & "'"
    Dim Cmd2 As OleDbCommand = New OleDbCommand(OledbStr, Cn2)
    Dim dr2 As OleDbDataReader = Cmd2.ExecuteReader()

    While dr2.Read
    Dim PropNode As New ViewNode((Trim(dr2("Adr1")) & "," & Trim(dr2("Adr2"))), dr2("Proprty"), 0)
    LocNode.Nodes.Add(PropNode)
    <<< Sets Child Node >>>
    OledbStr = "Select * from PropUnitAvailable where Proprty = " & CStr(dr2("Proprty"))
    Dim Cmd3 As OleDbCommand = New OleDbCommand(OledbStr, Cn3)
    Dim dr3 As OleDbDataReader = Cmd3.ExecuteReader()
    Dim Headline As String
    While dr3.Read
    If IsDBNull(dr3("Headline")) Then
    Headline = "No Headline"
    Else
    Headline = dr3("Headline")
    End If

    Dim UnitNode As New ViewNode((Trim(dr3("PropertyAdr")) & "," & Trim(Headline)), dr3("Proprty"), dr3("Unit"))
    PropNode.Nodes.Add(UnitNode)
    <<< Sets Grandchild Node >>>
    End While
    dr3.Close()
    Cmd3 = Nothing
    End While
    dr2.Close()
    Cmd2 = Nothing

    End While
    TreeView1.EndUpdate()
    Cmd1 = Nothing
    Cn1.Close()
    Cn2.Close()
    Cn3.Close()

    End Sub


    In this particular case the nesting is 3 levels but using this technique there is no real limit to the depth of the treeview


    Hope this helps.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hopefully Richard's code will take care of you otherwise here is some info on converting between a flat structure and a heirarchy structure.

    http://www.vbcity.com/forums/topic.asp?tid=56326

  4. #4
    Junior Member ost's Avatar
    Join Date
    Jan 2004
    Posts
    19
    I had the same problem a couple of days ago... this example helped med a lot

    Treeview Interface

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