[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


DataGridView RowPrePaint - Paint rows based on Cell value

Posted on 2009-12-16
Medium Priority
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:Tom Sage

Expert Comment

ID: 26067221

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


Accepted Solution

mohd_haq earned 2000 total points
ID: 26067241
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

by:Tom Sage
ID: 31667046
Your example was very helpful.  I was able to make the code in my program work as I wanted.

Thank you !

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 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