-1

我一直在使用以下代码更改 datagridview 中的单元格值颜色,没有任何问题,直到我将其传输到新项目并将项目连接到不同的服务器。它现在抛出“对象引用未设置为对象的实例”错误。我错过了什么?

它从 GridViewTextBoxColumn1 开始工作,直到它向下移动到 GridViewTextBoxColumn2 并且调试器开始抛出类似的错误。值不能转换为日期时间类型,然后对象引用未设置等。但我到处检查,一切似乎都到位,不明白它是如何以日期时间结束的。

 Private Sub TableGrid_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles TableGrid.CellFormatting

    For i As Integer = Nothing To Me.TableGrid.Rows.Count - 1
        If Me.TableGrid.Rows(i).Cells("GridViewTextBoxColumn1").Value <= 0 Then
            Me.TableGrid.Rows(i).Cells("GridViewTextBoxColumn1").Style.ForeColor = Color.Red
        End If
    Next
    For j As Integer = Nothing To Me.TableGrid.Rows.Count - 1
        If Me.TableGrid.Rows(j).Cells("GridViewTextBoxColumn2").Value.ToString = "S" Then
            Me.TableGrid.Rows(j).Cells("GridViewTextBoxColumn2").Style.ForeColor = Color.Blue
        End If
    Next

    For k As Integer = Nothing To Me.TableGrid.Rows.Count - 1
        If Me.TableGrid.Rows(k).Cells("GridViewTextBoxColumn3").Value.ToString = "Z" Then
            Me.TableGrid.Rows(k).Cells("GridViewTextBoxColumn3").Style.ForeColor = Color.Blue
        End If
    Next

End Sub
4

3 回答 3

0

找到了一个对我自己的问题有效的解决方案。但我相信应该有更好的答案。

我的第一个问题中的代码没有测试某些单元格中存在的空值。因此,错误..

For i As Integer = 0 To Me.TableGrid.Rows.Count - 1
If Not Me.TableGrid.Rows(i).Cells("DataGridViewTextBoxColumn1").Value Is DBNull.Value Then
If Me.TableGrid.Rows(i).Cells("DataGridViewTextBoxColumn1").Value = "Z" Then
Me.TableGrid.Rows(i).Cells("DataGridViewTextBoxColumn1").Style.ForeColor = Color.Blue
End If
End If

        If Not Me.TableGrid.Rows(i).Cells("DataGridViewTextBoxColumn2").Value Is DBNull.Value Then
            If Me.TableGrid.Rows(i).Cells("DataGridViewTextBoxColumn2").Value <0 Then
                Me.TableGrid.Rows(i).Cells("DataGridViewTextBoxColumn2").Style.ForeColor = Color.Red
            End If
        End If
于 2013-02-16T10:09:47.023 回答
0

尝试“Dim yourobject As New Yourtype”,因为抛出此异常时通常需要“New”,

于 2013-02-20T05:53:10.983 回答
0

通常在尝试引用尚未分配任何内容的对象时会发生此错误,因此请确保您的数据网格单元格中确实有值。

于 2013-02-18T16:41:37.460 回答