Results 1 to 6 of 6

Thread: [RESOLVED] Best way to load treeview from database.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] Best way to load treeview from database.

    What is the best to load a treeview from a database where it goes 4 deep

    Code:
    Parent
        child
        child
            grandchild
            grandchild
        child
        child
            grandchild
                great grandhcild
            grandchild
    How would that look in a database? and then how would you load the tree. I can load everything up to the grandchild, but as soon as I load the great grandchild I start having issues. So I am thinking it is the way they are in the database.

    Anybody have any examples they can share?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Best way to load treeview from database.

    It depends on the data. It might be stored in four different tables with each record having a foreign key from the table above or it might all be stored in one table with each record containing the ID of its parent. In the second case, loading the data might look like this:
    vb.net Code:
    1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.     Dim table As New DataTable
    3.  
    4.     'Populate table here.
    5.  
    6.     AddNodes(Me.TreeView1.Nodes, table, Nothing)
    7. End Sub
    8.  
    9. Private Sub AddNodes(nodes As TreeNodeCollection, table As DataTable, parentID As Integer?)
    10.     Dim filter = If(parentID.HasValue, "ParentID = " & parentID, "ParentID IS NULL")
    11.     Dim rows = table.Select(filter)
    12.  
    13.     For Each row In rows
    14.         Dim id = row.Field(Of Integer)("ID")
    15.         Dim node = nodes.Add(id.ToString(), row.Field(Of String)("Name"))
    16.  
    17.         'Make a recursive call to add child nodes to this node.
    18.         AddNodes(node.Nodes, table, id)
    19.     Next
    20. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Best way to load treeview from database.

    The latter is what I have. But it doesn't work when I have grand children. I mean it works but I get repeats of ones that don't belong there. I just wanted to make sure I was doing it correctly.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Best way to load treeview from database.

    Either you code is wrong or your data is wrong. We haven't seen either so we can't really help.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Best way to load treeview from database.

    its not the most elegant way after seeing your sample code.

    http://www.vbforums.com/showthread.php?t=662713

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Best way to load treeview from database.

    I think I got it. I converted your code to work with mine and it solved the problem. I don't have any bleed over. I knew it was the way I was loading the tree and seeing your code I seen my problem. More efficient too, lol

    Thank you JM, much appreciated to lead me in the right direction.

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