根据日期更改datagridview颜色

时间:2013-12-20 19:29:14

标签: vb.net datagridview

我有一个datagridview,显示数据库中的数据(MS Access)

datagridview正确显示数据..

现在我想将dgvReminder行的颜色更改为黄色,如果当前日期少于2天到Date_Of_Pickup。

Date_Of_Pickup采用此格式= 19-Dec-2013

到目前为止,我已经测试了这段代码: -

Private Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To dgvReminder.Rows.Count - 1
            If dgvReminder.Rows(i).Cells("Quantity").Value < 2 Then
                dgvReminder.Rows(i).Cells(2).Style.BackColor = Color.Yellow
            End If
        Next
End Sub

值小于2的整个数量列变为黄色

但是如何使用Date_Of_Pickup列?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

If DateDiff(DateInterval.Day,dgvReminder.Rows(i).Cells("Date_Of_Pickup").Value,Now()) > 2) Then
     dgvReminder.Rows(i).Cells(2).Style.BackColor = Color.Yellow
End If

答案 1 :(得分:0)

我做了这个并且它有效,我只是检查记录是否已过期或即将在正在进行的月份内到期。您可以根据需要更改日期间隔。我使用了Data GridView的DataBinding Complete事件

Private Sub grdMembersInfo_DataBindingComplete(sender As System.Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles grdMembersInfo.DataBindingComplete
    For i = 0 To grdMembersInfo.Rows.Count - 1
        Dim expDate As Date = grdMembersInfo.Rows(i).Cells("iCardExpiryDate").Value
        If DateDiff(DateInterval.Month, Date.Now, expDate) <= 0 Then
            grdMembersInfo.Rows(i).DefaultCellStyle.BackColor = Color.LightPink
        Else
            grdMembersInfo.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
        End If
    Next
End Sub