Like the title says, I'm trying to coloring an empty cell in bounded datagridview.
I've got this code to coloring a cell with specitic value and is working well.
This code is in the DatagridviewCellFormattingEventargs.

If e.ColumnIndex = 5 AndAlso e.Value <= Date.Now.ToShortDateString Then
              e.CellStyle.ForeColor = Color.DarkGreen
              e.CellStyle.Font = New Font("Adobe Caslon Pro", 11, FontStyle.Bold)
End If

How can I change this code that in the same column and the cell is empty the backgroundcolor becomes red?

Thanks in advance.

Recommended Answers

All 5 Replies

From memory you set the background color instead of the forecolor. I'm on my machine without VB.net so I won't check this out for quite some time.

rproffitt that is not what I meant.
The code about te Forecolor is working great. The text is coloring Darkgreen.
What I want is, if there is no text inside a cell in the same column the background color of those cells will be Red.

Again from memory for the reason given, I recall setting the background color even if there is no text in a cell. You can also do more research on this but I do recall doing this before and it was just one thing. However given the half dozen versions of vb.net maybe that was then and now is now?

PS. https://social.msdn.microsoft.com/Forums/windows/en-US/c221ce86-5fb8-4f79-bfa2-caeb90437fee/change-a-cell-color-based-on-contents-in-datagridview-vbnet?forum=winforms notes backcolor use.

Found a solution.
Here is my code:

   If e.ColumnIndex = 5 AndAlso Len(e.Value) > 0 Then

            e.CellStyle.BackColor = Color.FromArgb(235, 255, 238)
            e.CellStyle.ForeColor = Color.FromArgb(0, 100, 50)
            e.CellStyle.Font = New Font("Adobe Caslon Pro", 12, FontStyle.Italic)

        ElseIf e.ColumnIndex = 5 AndAlso Len(e.Value) <= 0 Then

            e.CellStyle.BackColor = Color.FromArgb(175, 255, 255)

        End If

Thanks for helping.

Use this to Format a "Rnage" of cells:

Function Cell_Font(SHEET as Worksheet, Rng, _
                     Optional Bold As Boolean = False, _
                     Optional Italic As Boolean = False, _
                     Optional Underline As XlUnderlineStyle = xlUnderlineStyleNone)
    ' Set indicated Cell Font.
    ' 1/31/17 Created. WML
    ' 1/28/21 Reworked. WML

    Prog = "Cell_Font"

    Sheet_Name = SHEET.Name

    SHEET.Range(Rng).Select
        Selection.Font.Bold = Bold
        Selection.Font.Italic = Italic
        Selection.Font.Underline = Underline

End Function ' Cell_Font()
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.