2013-02-09 73 views
-1

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

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

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 

回答

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 
0

尝试引用已经没有分配给它却又那么请确保您的数据网格细胞实际上对他们有什么价值对象时,通常会出现此错误。

+0

嗨,谢谢你的提示。下面的代码对具有空值的单元格起作用,并只将其中的值加亮显示。 – Bladefreak 2013-02-18 16:55:00

0

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

相关问题