Datagridview:如何更改隔天具有单元格值的交替行的颜色?
Datagridview: How to change color of alternate rows having cell value alternate days?
尊敬的先生,
我有一个 datagridview,在一列中填充了具有不同时间戳的数据
并尝试像上图那样在隔日范围内为行着色。到目前为止,我正在尝试这样
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
有什么建议吗?
您忠实的
穆鲁利马达夫
按照这些思路可能会满足您的要求:
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
这假定单元格按日期顺序排序并且值可转换为日期。它会在日期更改时交替显示,而不是每隔一天显示一次,但会在查看不同日期时突出显示。
尊敬的先生, 我有一个 datagridview,在一列中填充了具有不同时间戳的数据
并尝试像上图那样在隔日范围内为行着色。到目前为止,我正在尝试这样
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
有什么建议吗?
您忠实的 穆鲁利马达夫
按照这些思路可能会满足您的要求:
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
这假定单元格按日期顺序排序并且值可转换为日期。它会在日期更改时交替显示,而不是每隔一天显示一次,但会在查看不同日期时突出显示。