【问题标题】:compare two datagridview tables by specific column按特定列比较两个datagridview表
【发布时间】:2021-01-22 10:41:13
【问题描述】:

美好的一天!
我需要比较特定列上的两个 DataGridView 表,并在datagridview3 中显示匹配项,在datagridview4 中显示差异。
到目前为止,这就是我所做的一切。我可以在消息中显示:
告诉我如何更改代码。

For Each rw1 As DataGridViewRow In DataGridView1.Rows
    For Each rw2 As DataGridViewRow In DataGridView2.Rows
        If rw1.Cells(3).Value = rw2.Cells(3).Value Then
            If rw1.Cells(3).Value IsNot Nothing Then
                MsgBox(rw1.Cells(3).Value & "  in dgv2")
            End If
        End If
    Next
Next

表 1 中的数据:

Surname Name address id
Surname1 Name1 Adress1 14526
Surname2 Name2 Adress2 75856
Surname3 Name3 Adress3 36514
Surname4 Name4 Adress4 78425
Surname5 Name5 Adress5 36178
Surname6 Name6 Adress6 98317

表 2 中的数据:

Surname Name address id
Surname3 Name3 Adress3 36514
Surname5 Name5 Adress5 36178
Surname2 Name2 Adress2 75856
Surname6 Name6 Adress6 98317

我需要在datagridview3 中按列表(索引为3 的列)显示匹配项,在datagridview4 中按列表显示分歧。

非常感谢您的帮助

【问题讨论】:

  • 根据您想要通用解决方案的评论 - DataTables 有一个 Columns 属性集合,您可以在 For Each 循环中使用,就像 Rows 一样
  • 我可以提供一个代码示例吗,具体针对我的情况?

标签: vb.net winforms datagridview


【解决方案1】:

使用 Linq Linq 完成查找匹配和不匹配条目的繁重工作,然后循环遍历单元格并在目标 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

这只是循环遍历 DataGridViewRow 中的所有单元格,在适当的目标(dgvMatching 或 dgvNotMatching)DataGridViewRow 中设置值。它适用于任意数量的单元格/列。

【讨论】:

    【解决方案2】:

    您可以按照以下方式代替您的 MsgBox

        Dim row As String() = New String() {rw1.Cells(0).Value, rw1.Cells(1).Value, rw1.Cells(2).Value, rw1.Cells(3).Value}
        DataGridView3.Rows.Add(row) 
    

    然后按照相同的想法在else

    之后的DataGridView4中产生分歧

    【讨论】:

    • 是的,我试过这样,但我需要一个通用的解决方案。由于列数可能不同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2021-05-26
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多