如何根据日期更改datagridview单元格颜色 [英] How to change datagridview cell color base on date
问题描述
我希望在单元格数据<时将我的datagridview列CoverExpire颜色更改为红色。当前日期或前15天。
以下我提到了我的尝试。它没有改变我的datagridview上的任何内容
请帮我解决问题
我有什么试过:
Private Sub dgInsuranceSummary_DashBoard_CellFormatting(sender As Object,e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)处理dgInsuranceSummary_DashBoard.CellFormatting
对于i as Integer = 0到dgInsuranceSummary_DashBoard.Rows.Count - 1
如果dgInsuranceSummary_DashBoard.Rows(i).Cells(CoverExpire)。值< = CDate(现在)然后
dgInsuranceSummary_DashBoard.Rows(i).Cells(CoverExpire)。Style.ForeColor = Color.Red
dgInsuranceSummary_DashBoard.Rows(i)。 DefaultCellStyle.BackColor = Color.Blue
结束如果
下一页
结束Sub
I want to change my datagridview column "CoverExpire" color to red when cell data < current date or 15 days before.
below i mentioned what i have tried. it's not changing anything on my datagridview
Please help me to slove
What I have tried:
Private Sub dgInsuranceSummary_DashBoard_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgInsuranceSummary_DashBoard.CellFormatting
For i As Integer = 0 To dgInsuranceSummary_DashBoard.Rows.Count - 1
If dgInsuranceSummary_DashBoard.Rows(i).Cells("CoverExpire").Value <= CDate(Now) Then
dgInsuranceSummary_DashBoard.Rows(i).Cells("CoverExpire").Style.ForeColor = Color.Red
dgInsuranceSummary_DashBoard.Rows(i).DefaultCellStyle.BackColor = Color.Blue
End If
Next
End Sub
推荐答案
好像,你走在正轨......
检查一下:DataGridView.CellFormatting事件(System.Windows.Forms)| Microsoft Docs [ ^ ]
您无需使用...
循环。您需要的只是使用传递给事件的参数。注意:您必须检查单元格中的值是否不是Nothing
并且它包含Date
值。请参阅:
Seems, you're on right track...
Check this: DataGridView.CellFormatting Event (System.Windows.Forms) | Microsoft Docs[^]
You don't need to usefor...next
loop. All what you need is to use parameters passed to the event. Note: you have to check if value in a cell is notNothing
and it contains aDate
value. See:
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) Handles dataGridView1.CellFormatting
If Me.dataGridView1.Columns(e.ColumnIndex).Name _
= "CoverExpire" Then
If e.Value IsNot Nothing Then
' Check for the string "pink" in the cell.
Dim myDate As Date = CType(e.Value, Date)
Dim compDate As Date = DateTime.Today.AddDays(-15)
If myDate < compDate Then
e.CellStyle.BackColor = Color.Red
Else
e.CellStyle.BackColor = Color.Blue
End If
End If
End If
End Sub
注意:上面的代码可能包含错误,因为我还没有测试过。
也检查其他事件: DataGridView类(System.Windows.Forms)| Microsoft Docs [ ^ ]
date1在datagridview中,日期是今天的日期
稍微调整一下,比如forecolor
date1 is in datagridview , date is date today
Adjust a little bit , like forecolor
Try
For Each NRow In Me.TblgetalDataGridView.Rows
Dim date1, date2
date1 = CType(NRow.Cells(1).Value(), String)
If NRow.Cells(1).Value IsNot DBNull.Value Then
If DateDiff(DateInterval.Day, date1, date2) + 1 >= 15 Then
NRow.DefaultCellStyle.ForeColor = Color.Red
Else
NRow.DefaultCellStyle.ForeColor = Color.Black
End If
Else
End If
Next
Catch ex As Exception
End Try
<pre lang="vb">
Private Sub dgInsuranceSummary_DashBoard_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgInsuranceSummary_DashBoard.CellFormatting
For i As Integer = 0 To dgInsuranceSummary_DashBoard.Rows.Count - 1
''datediff(interval, date1, date2);
''interval can be day, month, year, hours, second minutes, etc. It subtracts date1 from ''date2.
''Enter date1 and date2 in Dateformat.
''Format: DateDiff(DateInterval.Day, Now.Date, Now.AddDays(4).Date)
''Output: 4
d =DateDiff(DateInterval.Day, Now.Date, Now.AddDays(-15).Date), cdate(now))
If dgInsuranceSummary_DashBoard.Rows(i).Cells("CoverExpire").Value <= CDate(Now) and d Then
dgInsuranceSummary_DashBoard.Rows(i).Cells("CoverExpire").Style.ForeColor = Color.Red
dgInsuranceSummary_DashBoard.Rows(i).DefaultCellStyle.BackColor = Color.Blue
End If
Next
End Sub
这篇关于如何根据日期更改datagridview单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!