这段代码有什么问题?我有3个表,与VisitorSheet相关的Building,以及与Visit相关的访问者表。但是并非所有建筑物都有VisitorSheet,并非所有VisitorSheet都有访问。运行此代码时,将为每个Building显示所有VisitorSheets,并为每个VisitorSheet显示所有Visits。如何过滤以在每个Treeview节点中显示正确的记录?
Me.VisitTableAdapter.Fill(Me.VisitDbDataSet.Visit)
Me.VisitorSheetTableAdapter.Fill(Me.VisitorSheetDbDataSet.VisitorSheet)
Me.BuildingTableAdapter.Fill(Me.BuildingDbDataSet.Building)
Try
For Each masterRow As DataRow In BuildingDbDataSet.Tables("Building").Rows
Dim masterNode As New TreeNode(masterRow("Abbr").ToString())
TreeView1.Nodes.Add(masterNode)
For Each childRow As DataRow In VisitorSheetDbDataSet.Tables("VisitorSheet").Rows
masterRow.GetChildRows("SheetNo")
Dim childNode As New TreeNode(childRow("SheetDate").ToString())
masterNode.Nodes.Add(childNode)
For Each childRow1 As DataRow In VisitDbDataSet.Tables("Visit").Rows
masterRow.GetChildRows("VisitNo")
Dim childNode1 As New TreeNode(childRow1("Visitor").ToString())
childNode.Nodes.Add(childNode1)
Next
Next
Next
样品
答案 0 :(得分:0)
您当前正在迭代每个表中的所有行。您只需迭代子行。
For Each masterRow As DataRow In BuildingDbDataSet.Tables("Building").Rows
Dim masterNode As New TreeNode(masterRow("Abbr").ToString())
TreeView1.Nodes.Add(masterNode)
For Each childRow As DataRow In VisitorSheetDbDataSet.Tables("VisitorSheet").Rows
'masterRow.GetChildRows("SheetNo")
Dim childNode As New TreeNode(childRow("SheetDate").ToString())
masterNode.Nodes.Add(childNode)
For Each childRow1 As DataRow In masterRow.GetChildRows("SheetNo")
'...
Next
Next
Next