简体   繁体   中英

How can i change color of particular row in datagridview, where a cell value is first timestamp of each day?

I have a datagridview filled with series of reports ordered in date and time for about an year
List is like below
27/1/2015 10:56:32 AM
27/1/2015 11:56:41 AM
27/1/2015 12:54:42 PM
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

How can I highlight or change color of that particular row, where each new day series begins? i mean highlighting row

where new day begins, 27, 28th etccc. So far i am trying like this

    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

but not getting any idea to proceed futher. any guidence?

I don't think you need to compute anything. I would change the color of the first row then I would loop all rows and compare the date with the previous row. If the day are different then I would change the row color.

Something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM