Hello! I'm working with a DataGridView that I want to format based on the condition of whether or not a date has passed. For example: If the date in a cell is past the current date, that cell and its row are formatted with the forecolor red. My current code is below, and while it doesn't present me with any errors, it just plain doesn't do anything! Any help would be greatly appreciated!

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting


        If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Due Date" Then

            If e.Value IsNot Nothing Then
                Dim dgvdate As String = e.Value.ToString
                ' MsgBox(dgvdate)
                If dgvdate = "5" Then
                    e.CellStyle.ForeColor = Color.Red
                Else
                    e.CellStyle.ForeColor = Color.Green
                End If
            End If
        End If
    End Sub

Recommended Answers

All 4 Replies

I would test if adding a FormattingApplied to true is enough:

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting


        If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Due Date" Then

            If e.Value IsNot Nothing Then
                Dim dgvdate As String = e.Value.ToString
                ' MsgBox(dgvdate)
                If dgvdate = "5" Then
                    e.CellStyle.ForeColor = Color.Red
                Else
                    e.CellStyle.ForeColor = Color.Green
                End If
                e.FormattingApplied = True
            End If
        End If
    End Sub

Maybe you'll need to also format the value manually.

Hope this helps

I appreciate the help, unfortunately this still doesn't work. I've changed my code a bit and here's what I have now:

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting


        If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Due Date" Then

            If e.Value IsNot Nothing Then
                Dim dgvdate As Date = CDate(e.Value)
                ' MsgBox(dgvdate)
                If dgvdate < CDate(Now) Then
                    e.CellStyle.BackColor = Color.Red
                    e.FormattingApplied = True
                Else
                    e.CellStyle.BackColor = Color.Green
                    e.FormattingApplied = True
                End If

            End If
        End If
    End Sub

Thanks again to anyone who can help!

Here's what ended up working, in case anyone else needs to know how to do this!

For i As Integer = 0 To Me.dgvOne.Rows.Count - 1
            If Me.dgvOne.Rows(i).Cells("SCHFINISH").Value < CDate(Now) Then
                Me.dgvOne.Rows(i).Cells("SCHFINISH").Style.ForeColor = Color.Red
                Me.dgvOne.Rows(i).DefaultCellStyle.BackColor = Color.Blue
            End If

This checks the datagridview (AFTER IT'S POPULATED) for the column "SCHFINISH" and compares it to the current date. If the value in the "SCHFINISH" cell is past, it colors the text to red and the background to blue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.