Change the datagridview colour based on date

时光总嘲笑我的痴心妄想 提交于 2020-01-11 13:15:07

问题


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 dgvReminder's row to yellow if current date is less than 2 days to the Date_Of_Pickup.

Date_Of_Pickup is in this format = 19-Dec-2013

So far i have test this code :-

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

The whole Quantity column which has value less than 2 turn to yellow

but how can i do this with the Date_Of_Pickup column??


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/20710688/change-the-datagridview-colour-based-on-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!