Datagridview:如何更改具有单元格值的备用行的颜色?

时间:2015-01-30 13:02:51

标签: vb.net

尊敬的先生, 我有一个datagridview填充了一列中具有各种时间戳的数据

enter image description here

尝试在隔天的范围内对行进行着色,如上图所示。到目前为止,我正在尝试这样的

Private Sub alternateDaysRows()
        For i As Integer = 1 To Me.datagridview1.Rows.Count - 1
            If i > 1 Then
                Dim myLastRowDate As Date
                myLastRowDate = CType(Me.datagridview1.Rows(Me.datagridview1.Rows.Count - 1).Cells(2).Value, DateTime).Date
                myCountDate = myLastRowDate.AddDays(1)
                For Each row As DataGridViewRow In Me.datagridview1.Rows
                    If CType(row.Cells(2).Value, DateTime).Date = myCountDate Then
                        row.DefaultCellStyle.BackColor = Color.WhiteSmoke
                    End If
                Next
            End If
        Next
    End Sub

有什么建议吗?

你的忠实 Murulimadhav

1 个答案:

答案 0 :(得分:1)

沿着这些方向的东西可以做你想要的:

Private Sub alternateDaysRows()
    Dim myLastRow As DataGridViewRow = Nothing
    Dim myLastColor = Color.Yellow
    Dim isFirstRow As Boolean = True

    For Each row As DataGridViewRow In Me.DataGridView1.Rows
        If myLastRow IsNot Nothing Then
            If DateTime.Parse(myLastRow.Cells(0).Value.ToString).Date <> DateTime.Parse(row.Cells(0).Value.ToString).Date Then
                myLastColor = If(myLastColor = Color.Yellow, Color.Red, Color.Yellow)
            End If
        End If

        myLastRow = row
        row.DefaultCellStyle.BackColor = myLastColor
    Next

这假设单元格按日期顺序排序,并且值可以转换为日期。它会在日期更改时而不是每隔一天更改一次,但会在查看其他日期时突出显示。