在循环中比较DataGridView列

时间:2019-05-16 16:25:17

标签: vb.net

我从Excel文件导入数据,并且需要从2列中连接字符串值;如果它们不同,则将两个值都放在最后一列中,并更改字符串的颜色。我可以收集在DataGridView中显示的行,但是我不确定在哪里比较每行中的列以及如何比较它们。

我尝试了2个这样的循环来遍历行和列;

    For Each row As DataGridViewRow In dgvTest.Rows

        For Each column As DataGridViewColumn In dgvTest.Columns
  Do While Not xlWorkSheet.Cells(i, 1).value = Nothing
            Dim poid As Integer
            Dim poid2 As Integer






                Dim row As String() = New String() {xlWorkSheet.Cells(i, 1).value, xlWorkSheet.Cells(i, 2).value, xlWorkSheet.Cells(i, 3).value, xlWorkSheet.Cells(i, 4).value,
                        xlWorkSheet.Cells(i, 5).value, xlWorkSheet.Cells(i, 7).value, xlWorkSheet.Cells(i, 9).value, xlWorkSheet.Cells(i, 10).value, xlWorkSheet.Cells(i, 11).value, xlWorkSheet.Cells(i, 12).value, xlWorkSheet.Cells(i, 17).value}





                dgvTest.Rows.Add(row)




                i += 1

        Loop

1 个答案:

答案 0 :(得分:0)

这应该让您开始。

    For Each DgvRow As DataGridViewRow In DataGridView1.Rows
        If DgvRow.Cells("Column1").Value.ToString <> DgvRow.Cells("Column2").Value.ToString Then
            DgvRow.Cells("Column3").Value = DgvRow.Cells("Column1").Value.ToString & DgvRow.Cells("Column2").Value.ToString
            DgvRow.Cells("Column3").Style.BackColor = Color.Red
        End If
    Next