Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to Populate TreeView from multiple tables

How to Populate TreeView from multiple tables

Scheduled Pinned Locked Moved Visual Basic
questiondatabasetutorial
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jo_eylee
    wrote on last edited by
    #1

    How can I populate a Treeview from Ms Access database which contains tables of :- 1. Parent (parent_id, parent_name) 2. Child (child_id, child_name) 3. ParentChildRelation (id, parent_id, child_id) The reason that I cannot use single parent-child table is I have child with multiple parents and parent with multiple child (M-to-M). How do I solve this in populating a treeview? Any sample demo with source code? :confused:

    L 1 Reply Last reply
    0
    • J jo_eylee

      How can I populate a Treeview from Ms Access database which contains tables of :- 1. Parent (parent_id, parent_name) 2. Child (child_id, child_name) 3. ParentChildRelation (id, parent_id, child_id) The reason that I cannot use single parent-child table is I have child with multiple parents and parent with multiple child (M-to-M). How do I solve this in populating a treeview? Any sample demo with source code? :confused:

      L Offline
      L Offline
      Leah_Garrett
      wrote on last edited by
      #2

      I have done something similar to this. I made one table that contained the data I wanted all in one table, all in order. The data had a three level hierarcy (Parent - 1, child - 2, grandchild -3). So when I looped through the table I would check the level and create a node accordingly. Private Sub PopulateNavigationTree() TopicTreeView.BeginUpdate() TopicTreeView.Nodes.Clear() Dim parentNode As TreeNode Dim childNode As TreeNode 'Dim fullPath As String Dim iLevel As Integer = 0 Dim baseNode As TreeNode = TopicTreeView.Nodes.Add("Topics") ' init to valid values for safety parentNode = baseNode childNode = baseNode Dim myRow As DataRow If m_TreeDataSet.Tables.Count > 0 Then If m_TreeDataSet.Tables(0).Rows.Count > 0 Then For Each myRow In m_TreeDataSet.Tables(0).Rows iLevel = CType(myRow.Item("Level").ToString, Integer) If iLevel = 1 Then parentNode = baseNode.Nodes.Add(myRow.Item("Topic").ToString) ElseIf iLevel = 2 Then childNode = parentNode.Nodes.Add(myRow.Item("Topic").ToString) Else childNode.Nodes.Add(myRow.Item("Topic").ToString) End If Next End If Else ' TO DO: add error message here End If TopicTreeView.CollapseAll() baseNode.Expand() TopicTreeView.EndUpdate() End Sub

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups