-2

所以一切都在问题中,我有一个数据网格视图,他在他的行集合中被一个 foreach 分配,就像这样dataGridView1.Rows,我在第二个 if 中得到 null 类型的错误 for each

Sub DataColumnFirstDouble(ByRef dGridView As DataGridView, ByVal iCol As Integer)

    Dim bFirstRow As Boolean = False
    Dim sTemp As String = ""

    For Each RW As DataGridViewRow In dGridView.Rows

        If (bFirstRow) Then
            If (RW.Cells(iCol).Value.ToString() = sTemp) Then
                RW.Cells(iCol).Selected = True
                dGridView.CurrentCell.Style.BackColor = Color.LightGreen
                dGridView.CurrentCell.Style.ForeColor = Color.White
            End If
        End If

        sTemp = RW.Cells(iCol).Value.ToString()
        bFirstRow = True

    Next

End Sub

顺便说一句,Datagrid 填充了 1 个条目

LongString, Number, Number

Hello     , 8     , 8

当我单击新行时会发生该错误,并且在行离开事件时也会调用该函数

需要一些帮助

顺便说一句,我尝试做的是检查用户何时在长字符串空间中输入名称,该名称是数据库中的主唯一键,但似乎我找不到任何东西来处理它,所以我尝试解析它每个有时他离开行来检查是否有双

4

1 回答 1

1

目前尚不清楚错误在哪里,您应该调试以确定哪个变量确切为空。所以我假设它是RW.Cells(iCol).Value.

如果单元格中没有值,则它可能为空。这个意思是ToString行不通的。

    If (bFirstRow) Then
        If RW.Cells(iCol).Value IsNot Nothing AndAlso RW.Cells(iCol).Value.ToString() = sTemp Then
            RW.Cells(iCol).Selected = True
            dGridView.CurrentCell.Style.BackColor = Color.LightGreen
            dGridView.CurrentCell.Style.ForeColor = Color.White
        End If
    End If

您甚至可以检查是否RW.Cells(iCol)存在,也许它正在尝试获取行中不存在的单元格中的数据。

于 2013-09-26T16:01:50.240 回答