VB.net 使用父子关系用数据集填充 TreeView
我正在开发一个程序,该程序允许我在 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
我真正想要的是一个显示嵌套的父子对象的树视图。我不确定在这种情况下如何实现这一点......有什么想法吗?我找到了这篇文章,但没有更多线索...
谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种方法来解决我想做的事情。我认为因为数据集包含父子关系,所以我可以使用它们来构建我的树视图。
DataSet 包括两个属性:父关系和子关系。我依靠这些来确定它们在关系树中的位置。使用 if 语句,我首先填充父节点,因为顶部父节点没有父节点。然后我检查父节点是否有子节点,并填充它们,最后,我使用计数器来填充孙节点。
一如既往,如果有人有更好的主意,我会洗耳恭听:)
谢谢......
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.
As always, if anyone has a better idea, I'm all ears:)
Thanks....