产品过期时,Visual Basic数据网格会更改行颜色

时间:2022-11-21 20:44:04

I have a datagrid which is connected to ms access. I have a column named "Expiration date". I am making the row of the datagrid becomes red when a product has expired. I am having trouble with this code.

我有一个连接到ms访问的数据网格。我有一个名为“过期日期”的列。当产品过期时,我正在使datagrid的行变为红色。我在使用此代码时遇到问题。

 Private Sub BoticaDataGridView_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles BoticaDataGridView.DataBindingComplete
    For i = 0 To BoticaDataGridView.Rows.Count - 1
        Dim expDate As Date = BoticaDataGridView.Rows(i).Cells(5).Value
        Dim now = Date.Now
        If expDate > now Then
            BoticaDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.White
        Else
            BoticaDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Red
        End If
    Next
End Sub

It says that "Conversion from type 'Integer' to type 'Date' is not valid." Please help me.

它表示“从类型'整数'转换为'日期'类型无效。”请帮帮我。

1 个解决方案

#1


0  

The value

价值

BoticaDataGridView.Rows(i).Cells(5).Value

is not a date value for at least one row and cell 5 in your loop.

不是循环中至少一行和单元格5的日期值。

#1


0  

The value

价值

BoticaDataGridView.Rows(i).Cells(5).Value

is not a date value for at least one row and cell 5 in your loop.

不是循环中至少一行和单元格5的日期值。