Using Linq Linq to do the heavy lifting of finding matching and non-matching entries, it then befores a matter of looping through the cells and setting the values in the target DataGridViews
Dim dgv1Rows As List(Of DataGridViewRow) = DataGridView1.Rows.Cast(Of DataGridViewRow).ToList()
Dim dgv2Rows As List(Of DataGridViewRow) = DataGridView2.Rows.Cast(Of DataGridViewRow).ToList()
Dim matchingItems = dgv1Rows.Where(Function(x)
Return dgv2Rows.Any(Function(y) x.Cells(3).Value = y.Cells(3).Value)
End Function)
Dim notMatchingItems = dgv1Rows.Where(Function(x)
Return Not dgv2Rows.Any(Function(y) x.Cells(3).Value = y.Cells(3).Value)
End Function)
For Each rw1 As DataGridViewRow In matchingItems
Dim addedRowId As Integer = dgvMatching.Rows.Add()
Dim targetRow As DataGridViewRow = dgvMatching.Rows(addedRowId)
For Each cell As DataGridViewCell In rw1.Cells
targetRow.Cells(cell.ColumnIndex).Value = cell.Value
Next
Next
For Each rw1 As DataGridViewRow In notMatchingItems
Dim addedRowId As Integer = dgvNotMatching.Rows.Add()
Dim targetRow As DataGridViewRow = dgvNotMatching.Rows(addedRowId)
For Each cell As DataGridViewCell In rw1.Cells
targetRow.Cells(cell.ColumnIndex).Value = cell.Value
Next
Next
This simply loops through all the cells in the DataGridViewRow setting the value in the appropriate target (dgvMatching or dgvNotMatching) DataGridViewRow. It will work for any number of cells/columns.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…