Avatar of GaryHoather
GaryHoather
 asked on

How do I change the back color of a row in a datagridview control based on the value of a cell in the row?

I have found out how to change the back color of an individual cell in a datagridview control at runtime based on the cell's value and would like to expand this by changing the back color of the whole row based on a cell value in the row when loaded.

Can this be done?
Visual Basic.NET

Avatar of undefined
Last Comment
mayank_joshi

8/22/2022 - Mon
appari

try like this
if somecondition then
       datagridviewname.rows(rowindex).DefaultCellStyle.BackColor = Color.Coral
end if

Open in new window

GaryHoather

ASKER
Hi Appari

I can not seem to get the above code that you posted to do what I need to do.  Here is my current code:

    Private Sub DataGridView2_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView2.CellFormatting


        If DataGridView2.Columns(e.ColumnIndex).Name = "JOB_DETAILS_MILEAGE" Then

            If e.Value >= 10 Then

                e.CellStyle.BackColor = Color.Orange
            Else
                e.CellStyle.BackColor = Color.Beige
            End If

        ElseIf DataGridView2.Columns(e.ColumnIndex).Name = "driverIsADR" Then

            If e.Value = True Then
                e.CellStyle.BackColor = Color.Orange
            Else
                e.CellStyle.BackColor = Color.Beige
            End If

        ElseIf DataGridView2.Columns(e.ColumnIndex).Name = "driverScCleared" Then

            If e.Value = True Then
                e.CellStyle.BackColor = Color.Red
            Else
                e.CellStyle.BackColor = Color.Beige
            End If

        ElseIf DataGridView2.Columns(e.ColumnIndex).Name = "driverIsThinClientTrained" Then

            If e.Value = True Then
                e.CellStyle.BackColor = Color.Navy
            Else
                e.CellStyle.BackColor = Color.Beige
            End If

        ElseIf DataGridView2.Columns(e.ColumnIndex).Name = "driverIsLapTopTrained" Then

            If e.Value = True Then
                e.CellStyle.BackColor = Color.Olive
            Else
                e.CellStyle.BackColor = Color.Beige
            End If

        Else
            e.CellStyle.BackColor = Color.Beige

        End If
    End Sub

This only changes the colour of the individual cell that is referenced as the datagridview is populated.

What I am trying to do is change the back colour of each cell in the row based on the value of one cell in the row.

Any ideas?
Nasir Razzaq

1) you are using the CellFormatting event
2) You have not implemented the suggested code.

Try using the RowDataBound event to apply the row styles.
Your help has saved me hundreds of hours of internet surfing.
fblack61
GaryHoather

ASKER
Hi CodeCruiser

Sorry for the delay in responding I got called onto another project.  I have tried to use the code suggested by appari but it did not provide the solution that I was hoping to achieve.

I am developing the project in visual studio 2010 and the default datagridview control does not seem to contain the RowDataBound event that you mentioned above. Is it associated with the datagridview control?

The reason that I need to alter the back colour of a whole row based on the contents of a control cell within the row relates to an older version of the project that was developed about 15 years ago in VB6 .  The users are used to this display function and trying to replicxate it in VS2010 has not been as straightforward as I had hoped.

In the old program the highlighting of the row by back colour alerts the user to specific changes to a the data in a row and highlights the need to react to the data changes..  In this way the display of data becomes by exception and not all instances.

I can change the back colour of the control cell but not the whole row.  So if you have any suggestions I would apprecite them.
Nasir Razzaq

Ok. Try looping through the grid rows and changing the RowsDefaultCellStyle

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsdefaultcellstyle.aspx
ASKER CERTIFIED SOLUTION
mayank_joshi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.