Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 3-Sep-18 23:28pm

Seems, you're on right track...

Check this: DataGridView.CellFormatting Event (System.Windows.Forms) | Microsoft Docs[^]

You don't need to use for...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 not Nothing and it contains a Date value. See:
VB.NET
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


Note: Above code may contains errors, because i haven't tested it.

Check other events too: DataGridView Class (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
v2
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
 
Share this answer
 
v2
Comments
Member 13745089 6-Sep-18 1:43am    
Hi BASSIES, THis method works fine. Thanks for your help
<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
 
Share this answer
 
Comments
Member 13745089 4-Sep-18 4:16am    
Can you explain this code to me. it is not clear
Member 13745089 4-Sep-18 4:21am    
please tell me where this "d" came from?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900