Gridview根据日期更改背景颜色

时间:2013-02-20 17:07:15

标签: asp.net vb.net gridview

我想根据当前日期更改gridview上的背景颜色。下面的代码有效,但未来的日期将以蓝色开始,而不是明天的1天。我错过了代码中的某些内容,还是应该以不同的方式进行修改?

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 个答案:

答案 0 :(得分: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