Color code datagridview based on date conditions

Nessie

Member
Joined
Oct 14, 2011
Messages
20
Programming Experience
1-3
Hi All,
I am loading a datagrid with data that selects everything with an 'Open' status but I would like to color code the datagrid based on my 'TrioleOpenDate' column taken from the Access table:

If the opendate is <= 3 days old nothing changes
If the opendate is >= 4 but <= 6 days then the rows turn yellow
If the opendate is => 7 days the rows are red.

So far I have all rows turning red :(

For i As Integer = 0 To Me.DGView1.Rows.Count - 1
If Me.DGView1.Rows(i).Cells("TrioleOpenDate").Value < CDate(Now) Then
Me.DGView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Next

Again many thanks for all yur help.
 
Hi Nessie,

I use some similar and that is working, so perhaps you can use parts of it.

If DataGridView1.Rows(i).Cells(5).Value.ToString = "Planned" Then
If Date.Parse(DataGridView1.Rows(i).Cells(6).Value.ToString) < Date.Parse(Now.Date.ToString) Then
DataGridView1.Rows(i).Cells(5).Style.BackColor = Color.Red
DataGridView1.Rows(i).Cells(5).Style.ForeColor = Color.White
itotal = itotal + 1
Else
DataGridView1.Rows(i).Cells(5).Style.BackColor = Color.GreenYellow
DataGridView1.Rows(i).Cells(5).Style.ForeColor = Color.Black
End If
End If

Regards, Jan...
 
Sorted...

For i As Integer = 0 To Me.DGView1.Rows.Count - 1

If Date.Parse(DGView1.Rows(i).Cells("TrioleOpenDate").Value.ToString) < Date.Parse(Date.Now.AddDays(-3).ToString) Then
Me.DGView1.Rows(i).DefaultCellStyle.BackColor = Color.Red

For c As Integer = 0 To Me.DGView1.Rows.Count - 1

If Date.Parse(DGView1.Rows(i).Cells("TrioleOpenDate").Value.ToString) >= Date.Parse(Date.Now.AddDays(-7).ToString) Then
Me.DGView1.Rows(i).DefaultCellStyle.BackColor = Color.Yellow

End If
Next

End If
Next
 
Your Date values should really be Date values, and the need for conversion to and from String unnecessary:
If CType(DGView1("TrioleOpenDate",i).Value, Date) < Date.Now.AddDays(-3) Then
 
Back
Top