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. Populating Treeview using DataTable [modified]

Populating Treeview using DataTable [modified]

Scheduled Pinned Locked Moved Visual Basic
phpcomhostingdata-structureshelp
5 Posts 2 Posters 0 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.
  • K Offline
    K Offline
    klaydze
    wrote on last edited by
    #1

    hi, i have a little problem in populating my TreeView. i already populated all the Parent Node and its corresponding Child Node orderly. the problem is, when my form load where the treeview is place this is the output. i attached the picture to be able to understand well because my english is not that good. sorry for that. but anyway the ordering of the Parent and Child Node is correct. what i want is all the child node of the specific parent node will under it. this is my code Dim DA As clsDataAccess Dim DA2 As clsDataAccess Dim DT As DataTable Dim DT2 As DataTable Dim i As Integer Dim nRow As Integer Dim nRow2 As Integer Dim strParent As String Dim strChild As String Dim intParent As String Try DA = New clsDataAccess DT = DA.ExecQuery("SELECT DISTINCT(tblMicromixParentNode.intParentNodeID), dbo.tblMicromixParentNode.vchrDescription AS " & _ "vchrParentDesc,dbo.tblMicromixParentNOde.intOrder FROM dbo.tblMicromixMenu INNER JOIN dbo.tblMicromixParentNode ON " & _ "dbo.tblMicromixMenu.intParentNodeID = dbo.tblMicromixParentNode.intParentNodeID ORDER BY intOrder ASC") If DT.Rows.Count > 0 Then Dim PNode As Windows.Forms.TreeNode TV.Nodes.Clear() For nRow = 0 To DT.Rows.Count - 1 intParent = DT.Rows(nRow).Item("intParentNodeID") strParent = DT.Rows(nRow).Item("vchrParentDesc") PNode = TV.Nodes.Add(strParent) DA2 = New clsDataAccess DT2 = DA2.ExecQuery("SELECT t1.*, t2.vchrDescription AS vchrFormDesc, " & _ " t2.vchrFormName AS vchrFormName FROM " & _ " tblMicromixMenu t1 INNER JOIN tblMicromixChildNode " & _ " t2 ON t1.intChildNodeID = t2.intChildNodeID " & _ " where t1.intAccessID=1 and t1.intParentNodeID=" & intParent & " and intShow=1") If DT2.Rows.Count > 0 Then Dim CNode As TreeNode For nRow2 = 0 To DT2.Rows.Count - 1 strChil

    D 1 Reply Last reply
    0
    • K klaydze

      hi, i have a little problem in populating my TreeView. i already populated all the Parent Node and its corresponding Child Node orderly. the problem is, when my form load where the treeview is place this is the output. i attached the picture to be able to understand well because my english is not that good. sorry for that. but anyway the ordering of the Parent and Child Node is correct. what i want is all the child node of the specific parent node will under it. this is my code Dim DA As clsDataAccess Dim DA2 As clsDataAccess Dim DT As DataTable Dim DT2 As DataTable Dim i As Integer Dim nRow As Integer Dim nRow2 As Integer Dim strParent As String Dim strChild As String Dim intParent As String Try DA = New clsDataAccess DT = DA.ExecQuery("SELECT DISTINCT(tblMicromixParentNode.intParentNodeID), dbo.tblMicromixParentNode.vchrDescription AS " & _ "vchrParentDesc,dbo.tblMicromixParentNOde.intOrder FROM dbo.tblMicromixMenu INNER JOIN dbo.tblMicromixParentNode ON " & _ "dbo.tblMicromixMenu.intParentNodeID = dbo.tblMicromixParentNode.intParentNodeID ORDER BY intOrder ASC") If DT.Rows.Count > 0 Then Dim PNode As Windows.Forms.TreeNode TV.Nodes.Clear() For nRow = 0 To DT.Rows.Count - 1 intParent = DT.Rows(nRow).Item("intParentNodeID") strParent = DT.Rows(nRow).Item("vchrParentDesc") PNode = TV.Nodes.Add(strParent) DA2 = New clsDataAccess DT2 = DA2.ExecQuery("SELECT t1.*, t2.vchrDescription AS vchrFormDesc, " & _ " t2.vchrFormName AS vchrFormName FROM " & _ " tblMicromixMenu t1 INNER JOIN tblMicromixChildNode " & _ " t2 ON t1.intChildNodeID = t2.intChildNodeID " & _ " where t1.intAccessID=1 and t1.intParentNodeID=" & intParent & " and intShow=1") If DT2.Rows.Count > 0 Then Dim CNode As TreeNode For nRow2 = 0 To DT2.Rows.Count - 1 strChil

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It doesn't work because you didn't add your child nodes to any of the existing parent nodes at all. They're all children of the root. You're using lousy variable names by the way. It's very difficult to figure out what the variable is used for just be glancing at it. This section of code should be modified:

      If DT2.Rows.Count > 0 Then
      Dim CNode As TreeNode
      For nRow2 = 0 To DT2.Rows.Count - 1
      strChild = DT2.Rows(nRow2).Item("vchrFormDesc")
      PNode.Nodes.Add(strChild)
      Next
      End If

      You add the children to the parent node you created above it, not to the root of the TreeView nodes collection.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        It doesn't work because you didn't add your child nodes to any of the existing parent nodes at all. They're all children of the root. You're using lousy variable names by the way. It's very difficult to figure out what the variable is used for just be glancing at it. This section of code should be modified:

        If DT2.Rows.Count > 0 Then
        Dim CNode As TreeNode
        For nRow2 = 0 To DT2.Rows.Count - 1
        strChild = DT2.Rows(nRow2).Item("vchrFormDesc")
        PNode.Nodes.Add(strChild)
        Next
        End If

        You add the children to the parent node you created above it, not to the root of the TreeView nodes collection.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        K Offline
        K Offline
        klaydze
        wrote on last edited by
        #3

        first of all thank u for the reply sir. oh yes i see now. my code below is the same so i get an output that they become all Parent Node. so my problem now is how can i add the child node in a specific parent node? because in vb6 they have Relative,Relationship and Text. which is the Relative is the Parent Node and the Relationship is the word "tvwChild". how can i convert this line of code in VB6 to .Net TreeView1.Nodes.Add(Relative,Relationship,Key,Text,Image) where the Relationship should be "tvwChild" Parent Node For nRow = 0 To DT.Rows.Count - 1 intParent = DT.Rows(nRow).Item("intParentNodeID") strParent = DT.Rows(nRow).Item("vchrParentDesc") PNode = TV.Nodes.Add(strParent) Child Node If DT2.Rows.Count > 0 Then Dim CNode As TreeNode For nRow2 = 0 To DT2.Rows.Count - 1 strChild = DT2.Rows(nRow2).Item("vchrFormDesc") CNode = TV.Nodes.Add(strChild) Next End If sorry for my english. Thank You.

        Don't block the drive way of all the newbies in programming. :)

        D 1 Reply Last reply
        0
        • K klaydze

          first of all thank u for the reply sir. oh yes i see now. my code below is the same so i get an output that they become all Parent Node. so my problem now is how can i add the child node in a specific parent node? because in vb6 they have Relative,Relationship and Text. which is the Relative is the Parent Node and the Relationship is the word "tvwChild". how can i convert this line of code in VB6 to .Net TreeView1.Nodes.Add(Relative,Relationship,Key,Text,Image) where the Relationship should be "tvwChild" Parent Node For nRow = 0 To DT.Rows.Count - 1 intParent = DT.Rows(nRow).Item("intParentNodeID") strParent = DT.Rows(nRow).Item("vchrParentDesc") PNode = TV.Nodes.Add(strParent) Child Node If DT2.Rows.Count > 0 Then Dim CNode As TreeNode For nRow2 = 0 To DT2.Rows.Count - 1 strChild = DT2.Rows(nRow2).Item("vchrFormDesc") CNode = TV.Nodes.Add(strChild) Next End If sorry for my english. Thank You.

          Don't block the drive way of all the newbies in programming. :)

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Did you understand the changes I made to your code in my last post? You add a parent node to the root of the tree. Then you retrieve all the children of that node from the database and add those to the Nodes collection of the Parent you just added.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          K 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Did you understand the changes I made to your code in my last post? You add a parent node to the root of the tree. Then you retrieve all the children of that node from the database and add those to the Nodes collection of the Parent you just added.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            K Offline
            K Offline
            klaydze
            wrote on last edited by
            #5

            oh yes sir i do understand that. i just explaining my previous code. :) the code is working now. thank you very much. i wish you are open again when i have a difficulties in my code. Thank You.

            Don't block the drive way of all the newbies in programming. :)

            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