Populating Treeview using DataTable [modified]
-
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
-
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
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 IfYou 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 -
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 IfYou 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, 2007first 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. :)
-
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. :)
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 -
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, 2007oh 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. :)