DataGridView RowPrePaint - Paint rows based on Cell value

Posted on 2009-12-16
Last Modified: 2013-11-07
How can I color a row based on a cell value on that row using DataGridView RowPrePaint event?

Thank you
Question by:MisterT25
    LVL 3

    Expert Comment


    I have been doing this in the Datasource Changed sub of a datagrid. I am changing the background color based on the parents value. It shouldnt be hard to convert to what you are after.

    Hope this helps
    Private Sub dgvNodes_DataSourceChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvNodes.DataSourceChanged
            ' This changes the colour of a row.
            ' Changes colours when new powersource is used and lightens the colours when they are down the tree from the parent
            Dim CurrentRow As DataGridViewRow
            Dim ParentCell As DataGridViewTextBoxCell
            Dim LightenedColour As Color
            SourceColourIndex = 0
            ' Loops through all the rows in the datagrid
            For Each CurrentRow In dgvNodes.Rows
                ParentCell = dgvNodes(clnNodeFeedsFrom.Index, CurrentRow.Index)
                ' If the current node has no parent then it is a power source
                If ParentCell.Value = "" Then
                    ' Assign each cell in that row to one of the colours in the list
                    CurrentRow.DefaultCellStyle.BackColor = SourceColourList(SourceColourIndex)
                    ' Increment the colour index by one so no two consective rows have the same colour
                    SourceColourIndex = SourceColourIndex + 1
                    If SourceColourIndex > SourceColourList.Length - 1 Then
                        SourceColourIndex = 0
                    End If
                    ' The current row is not a power supply so find its parent and lighten the colour
                    LightenedColour = Lighten(FindParentColour(ParentCell.Value))
                    ' Assign each cell in that row to the new colour
                    CurrentRow.DefaultCellStyle.BackColor = LightenedColour
                End If
        End Sub

    Open in new window

    LVL 6

    Accepted Solution

    try this code
    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
            If Not e.IsLastVisibleRow = True Then
                If DataGridView1.Rows(e.RowIndex).Cells("Login_Id").Value.ToString() = "10004" Then
                    DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Aqua
                End If
            End If
        End Sub
    change as approprite. here i have used RowPostPaint event. i have changed the background color of the row to aqua for the login id = 10004. you can try your formating and logic but this should help you.

    Author Closing Comment

    Your example was very helpful.  I was able to make the code in my program work as I wanted.

    Thank you !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now