|
■No27396 (PROTO さん) に返信 > 同じ構造のDataTableが2つ(TableA、TableB)有り、TableAの列Aに含まれるデータと一致する > TableBの全てのレコードを抽出してTableCを作成したいと考えています。
これでよいのかな。
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim tblA As DataTable = CreateA() Dim tblB As DataTable = CreateB()
Dim ds As New DataSet() ds.Tables.Add(tblA) ds.Tables.Add(tblB) ds.Relations.Add("TableA_TableB", tblA.Columns("列A"), tblB.Columns("列A"), False)
DataGridView1.DataSource = ds DataGridView1.DataMember = "TableA"
DataGridView2.DataSource = ds DataGridView2.DataMember = "TableB"
tblA.Columns.Add("カウント", GetType(Integer), "Count(Child.列A)")
Dim dvA As New DataView(tblA) dvA.RowFilter = "カウント>0" DataGridView3.DataSource = dvA
Dim dvB As New DataView(tblB) dvB.RowFilter = "Parent.カウント>0" DataGridView4.DataSource = dvB
'Dim tblC As DataTable = dvB.ToTable() End Sub
Private Shared Function CreateA() As DataTable CreateA = New DataTable("TableA") With CreateA.Columns .Add("列A") .Add("列B") .Add("列C") End With With CreateA.Rows .Add("11", "21", "A1") .Add("12", "22", "A2") .Add("13", "23", "A3") .Add("14", "24", "A4") End With End Function
Private Shared Function CreateB() As DataTable CreateB = New DataTable("TableB") With CreateB.Columns .Add("列A") .Add("列B") .Add("列C") End With With CreateB.Rows .Add("11", "21", "B1") .Add("11", "22", "B2") .Add("13", "23", "B3") .Add("14", "24", "B4") .Add("21", "23", "B5") .Add("22", "24", "B6") End With End Function
|