开发者

VB.net Populate TreeView with DataSet using Parent-Child Relations

开发者 https://www.devze.com 2023-04-07 15:01 出处:网络
I\'m working on a program that allows me to edit XML data in a DataGridView.I have most everything working but I don\'t like my current TreeView Structure.

I'm working on a program that allows me to edit XML data in a DataGridView. I have most everything working but I don't like my current TreeView Structure.

I load the XML data into a DataSet and edit it there, so that is what I'd prefer to base my TreeView on. I've tried a few things such as..

Private Sub updateTree()
    'Clear All Previous TreeView Nodes
    TreeView1.Nodes.Clear()
    'Loop Through XML Nodes and Add them to the Tree
    For Each table As DataTable In ds.Tables
        Dim node As New TreeNode(table.TableName)
        If table.ChildRelations.Count = 0 Then
            node.Text = table.TableName
            node.Tag = table.TableName
            TreeView1.Nodes.Add(node)
        Else
            node.Tag = 开发者_开发知识库table.TableName
            node.Text = table.TableName & " - No Child Objects"
            TreeView1.Nodes.Add(node)
        End If
    Next
End Sub

What I'd really like to have is a tree view that shows the Parent Child objects nested. I'm not sure exactly how to accomplish that in this case... Any ideas?? I found this, article, but don't have many more leads...

Adding Nested Treeview Nodes in VB.NET?

Thanks.


I found a way to problematically accomplish what I was trying to do. I figured that because the DataSet includes the parent child relationships, I could use those to build my treeview.

A DataSet includes two Properties, parent relations and child relations. I the count on those to determine where they were at in the relationship tree. Using an if statement, I first Populate the Parent Node, because the top Parent has no Parent. Then I check to see if there is a child to the parent, and populate those, lastly, I use a counter to populate the grandchild node.

'Sub for calling a treeview update when needed
Private Sub updateTree()
    'Clear All Previous TreeView Nodes
    TreeView1.Nodes.Clear()
    'Loop Through the database examining the Parent child relationship and Add the nodes to the Tree
    Dim i As Integer = 0
    For Each table As DataTable In ds.Tables
        Dim node As New TreeNode(table.TableName)
        If table.ParentRelations.Count = 0 Then
            node.Text = table.TableName & " -Parent"
            node.Tag = table.TableName
            TreeView1.Nodes.Add(node)
        ElseIf table.ParentRelations.Count = 1 And table.ChildRelations.Count = 1 Then
            node.Tag = table.TableName
            node.Text = table.TableName & "-Child"
            TreeView1.Nodes(0).Nodes.Add(node)
        ElseIf table.ChildRelations.Count = 0 And table.ParentRelations.Count = 1 Then
            node.Tag = table.TableName
            node.Text = table.TableName & "-Grandchild"
            TreeView1.Nodes(0).Nodes(i).Nodes.Add(node)
            i += 1
        End If
    Next 

As always, if anyone has a better idea, I'm all ears:)

Thanks....

0

精彩评论

暂无评论...
验证码 换一张
取 消