VB.net 使用父子关系用数据集填充 TreeView

发布于 2024-12-06 18:30:09 字数 1005 浏览 1 评论 0原文

我正在开发一个程序,该程序允许我在 DataGridView 中编辑 XML 数据。我几乎一切正常,但我不喜欢我当前的 TreeView 结构。

我将 XML 数据加载到 DataSet 中并在那里进行编辑,因此我希望将其作为 TreeView 的基础。我尝试了一些事情,例如......

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

我真正想要的是一个显示嵌套的父子对象的树视图。我不确定在这种情况下如何实现这一点......有什么想法吗?我找到了这篇文章,但没有更多线索...

添加VB.NET 中的嵌套 Treeview 节点?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰認得朕 2024-12-13 18:30:09

我找到了一种方法来解决我想做的事情。我认为因为数据集包含父子关系,所以我可以使用它们来构建我的树视图。

DataSet 包括两个属性:父关系和子关系。我依靠这些来确定它们在关系树中的位置。使用 if 语句,我首先填充父节点,因为顶部父节点没有父节点。然后我检查父节点是否有子节点,并填充它们,最后,我使用计数器来填充孙节点。

'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 

一如既往,如果有人有更好的主意,我会洗耳恭听:)

谢谢......

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....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文