0

我正在尝试根据日期列按以下顺序标记网格中的行

  • 从今天起 2 天或以上时,则为 RED
  • 当 1 天大时,然后是黄色
  • 当 0 天大时为绿色
  • 当日期在未来时,则为蓝色

我有以下工作正常,除了未来的日期是绿色而不是蓝色。

 Dim myDate As DateTime = CType(grdSummaryView.GetRowCellValue(e.RowHandle, "myDate"), DateTime)

 Select Case Now.Subtract(myDate).Days
                '2 or more days old then RED FLAG
                Case Is >= 2
                    e.Value = ImageCollection2.Images(3)
                Case 1
                '1 day old then YELLOW FLAG
                    e.Value = ImageCollection2.Images(1)
                Case 0
                'Current day then GREEN FLAG
                    e.Value = ImageCollection2.Images(0)
                Case Else
                    e.Value = ImageCollection2.Images(4)
 End Select
4

3 回答 3

2

.Days 总是给出一个整数值,所以它在你至少 24 小时后才会起作用。您可以按照自己的建议解决它,也可以立即使用 Timespan 差异。

您可能还想考虑这种差异的含义。2 天前是否意味着您要选择 48 小时前创建的元素,或者它是否意味着例如在 11 月 10 日创建的所有条目。

于 2010-11-12T20:42:40.233 回答
1

我找到了解决方案。

我首先使用 Date.Compare 用 IF 包装我的 CASE,以首先检查日期是否在未来。

If Date.Compare(myDate, Now) < 0 Then
                Select Case Now.Subtract(delivDate).Days

                    Case Is >= 2
                        '2 or more days old then RED FLAG
                        e.Value = ImageCollection2.Images(3)
                    Case 1
                        '1 day old then YELLOW FLAG
                        e.Value = ImageCollection2.Images(1)
                    Case Else
                        '0 day (current day) then GREEN FLAG
                        e.Value = ImageCollection2.Images(0)
                End Select
            Else
                'DATE IS IN THE FUTURE
                e.Value = ImageCollection2.Images(4)
            End If
于 2010-11-12T20:31:45.883 回答
1

我可能会建议另一种编写代码的方法:

Dim age As Double = Now.Substract(myDate).TotalDays

If age >= 2 Then
  e.Value = ImageCollection2.Images(3) //Red
ElseIf age >= 1 Then
  e.Value = ImageCollection2.Images(1) //Yellow
ElseIf age >= 0 Then
  e.Value = ImageCollection2.Images(0) //Green
Else
  e.Value = ImageCollection2.Images(4) //Blue
End If

正如我在最初的评论中提到的,除非您在未来至少 24 小时,否则 Days 将返回 0。因此,如果它是 2010/08/15 12:30:00,而您的未来日期是 2010/08/16 0:30:00,那么 TimeSpan 为 -00:12:00:00 等,Days 将为 0。

于 2010-11-12T20:36:55.250 回答