我有一段代码从datagridview
读取整数和日期值。一些k
值包含空条目,并且我试图让应用程序忽略这些单元格,但我没有任何运气。该行上会弹出错误
j = datediff....
我尝试过使用if语句
If DataGridView1.Rows(e.RowIndex).Cells(k).Value IsNot Nothing Then
但它仍会产生错误,告诉我它无法将DBNull
条目转换为date
。
我无法看到我做错了什么,所以任何帮助都会受到赞赏。
For k = 8 To 52 Step 2
Dim j As Integer
If DataGridView1.Rows(e.RowIndex).Cells(k).Value IsNot Nothing Then
j = DateDiff(DateInterval.Day, DataGridView1.Rows(e.RowIndex).Cells(k).Value, DataGridView1.Rows(e.RowIndex).Cells(k - 2).Value)
If DataGridView1.Rows(e.RowIndex).Cells(k + 1).Value = 0 Then
If j > 7 Then
DataGridView1.Rows(e.RowIndex).Cells(k - 1).Value = 6
Else
End If
Else
End If
End If
Next k
答案 0 :(得分:0)
如果您希望在编译时识别可能的错误方面获得更多帮助,请在项目或文件中设置Option Strict On
。
Nothing
和DbNull
不一样。 Nothing
是DbNull
我们输入代表数据库NULL
的值。DbNull.Value
具有可比性。For k = 8 To 52 Step 2
Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If row.Cells(k).Value Is DBNull.Value Or row.Cells(k).Value Is Nothing Then Continue For
Dim firstDate As Date = DirectCast(row.Cells(k).Value, Date)
Dim secondDate As Date = DirectCast(row.Cells(k - 2).Value, Date)
Dim difference As TimeSpan = firstDate - secondData
If difference.Days > 7 AndAlso row.Cells(k + 1).Value = 0 Then
row.Cells(k - 1).Value = 6
End If
Next