问题
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