如何在datagridview中更改特定行的颜色,其中单元格值是每天的第一个时间戳?

时间:2015-01-02 18:32:57

标签: vb.net

我有一个数据网格视图,其中包含大约一年的日期和时间排序的一系列报告
列表如下
27/1/2015 10:56:32 AM
27/1/2015 11:56:41 AM
2015年1月27日下午12:54:42
28/1/2015 8:54:54 AM
28/1/2015 9:02:39 PM
29/1/2015 11:02:47 AM
29/1/2015 9:03:00 PM
30/1/2015 9:03:00 PM

如何突出显示或更改每个新系列开始的特定行的颜色?我的意思是突出显示行

新的一天开始,27,28th etccc。到目前为止,我正在尝试这样的

    Private Sub myFindRow()
            Dim sTime As DateTime = Me.myDataset.myReportTable.Compute("Max(reporttime)", "")
            Dim eTime As DateTime = Me.myDataset.myReportTable.Compute("Min(reporttime)", "")
            Dim cTime As DateTime = sTime
            For Each Day As DateTime In Enumerable.Range(0, (eTime - sTime).Days).Select(Function(i) sTime.AddDays(i))
                changeRowColor()
            Next Day
        End Sub

  Private Sub changeRowColor()
        For Each myRow As DataGridViewRow In Me.myDatagridView.Rows
            Dim myTime As DateTime
            myTime = myRow.Cells(2).Value
        Next
    End Sub

但没有任何想法继续前进。任何指导?

1 个答案:

答案 0 :(得分:3)

我认为你不需要计算任何东西。我会改变第一行的颜色然后我将循环所有行并将日期与前一行进行比较。如果这一天不同,那么我会更改行颜色。

像这样的东西

Private Sub myFindRow()
    ' Change the color of the first row

    For rowIndex As Integer = 1 To Me.myDatagridView.Rows.Count-1
        Dim curRow As DataGridViewRow = Me.myDatagridView.Rows(i)
        Dim prevRow As DataGridViewRow = Me.myDatagridView.Rows(i-1)

        If CType(curRow.Cells(2).Value, DateTime).Date <> CType(prevRow.Cells(2).Value).Date Then
            ' Change the color of row curRow
        End If
    Next
End Sub