2

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

4

1 回答 1

1

您可能希望使用截断时间的Date属性:DateTime

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
于 2013-02-20T17:15:39.203 回答