2
votes

I would like to change the background color on my gridview based on current date. The code below works, but the future date which will be colored in blue starts two days later instead of 1 day which is tomorrow. Am I missing something in the code or should it be modified differently? .

If e.Row.RowType = DataControlRowType.DataRow Then
                    Dim data As DateTime = Convert.ToDateTime(DirectCast(e.Row.DataItem, DataRowView)("Date").ToString())
                    Dim diff As TimeSpan
                    diff = DateTime.Now.Subtract(data)
                    Dim days As Integer = diff.Days
                    'Yellow = past date / White = current date / Blue = future date
                    If days > 0 Then
                        e.Row.BackColor = Drawing.ColorTranslator.FromHtml("#FFFFBB") 'past date'
                    ElseIf days < 0 Then
                        e.Row.BackColor = Drawing.ColorTranslator.FromHtml("#BAD8FF") 'future date'
                    ElseIf days = 0 Then 'present date
                        'do nothing
                    End If
                End If
1

1 Answers

1
votes

You might want to use the Date property of DateTime which truncates the time:

Dim data = DirectCast(e.Row.DataItem, DataRowView)
Dim day As Date = data.Row.Field(Of Date)("Date").Date
Dim daysDiff As Int32 = (Now.Date - day).Days