Change the datagridview colour based on date

前端 未结 2 1003
轮回少年
轮回少年 2021-01-26 10:09

i got a datagridview which displays data from database(MS Access)

the datagridview display the data correctly..

now i want to change the colour of the dgvRemind

相关标签:
2条回答
  • 2021-01-26 11:04

    try something like this:

    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
    
    0 讨论(0)
  • 2021-01-26 11:10

    I did this one and it works , I just checked if the record is expired or about to expire within ongoing month. you can just change the date interval as your need. I used DataBinding Complete event of Data GridView

    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
    
    0 讨论(0)
提交回复
热议问题