Change datagridview row color based on cell value

I want to change the color for a datagridview based on a cell value in that row. I just through this together and it only changes the first cells color. I see that but am not sure how to change the entire rows color.

 For i As Integer = 0 To Me.TblPersonsDataGridView1.Rows.Count - 1
            If Me.TblPersonsDataGridView1.Rows(i).Cells(64).Value >= "Yes" Then
                'Me.TblPersonsDataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Red
                Me.TblPersonsDataGridView1.Rows(i).Cells(i).Style.BackColor = Color.Red
            End If
        Next
LVL 13
wiswalldAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
You can use DefaultCellStyle to change the color of all row.
Check this small example and you can adapt to your code:

        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                If row.Cells(2).Value = "Yes" Then
                    row.DefaultCellStyle.BackColor = Color.Red
                End If
            End If
        Next

Open in new window

0
wiswalldAuthor Commented:
Sorry I was off for a few days. I need to account for a possible null value. I tried this.


        For Each row As DataGridViewRow In Me.TblPersonsDataGridView1.Rows
            If Not row.IsNewRow Then
                If row.Cells(64).Value Is DBNull.Value Then
                ElseIf row.Cells(64).Value Is "Yes" Then
                    row.DefaultCellStyle.BackColor = Color.Red
                End If
                End If
        Next
0
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way:

        For Each row As DataGridViewRow In Me.TblPersonsDataGridView1.Rows
            If Not row.IsNewRow Then
                If Not row.Cells(64).Value.ToString.IsNullOrEmpty Then
                    If row.Cells(64).Value Is "Yes" Then
                        row.DefaultCellStyle.BackColor = Color.Red
                    End If
                End If
            End If
        Next

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

wiswalldAuthor Commented:
Error      2      Argument not specified for parameter 'value' of 'Public Shared Function IsNullOrEmpty(value As String) As Boolean'.      C:\CAD_PRO_Plus\CAD_Pro_PlusX\CAD_Pro_PlusX\frmIncidentReport.vb      632      24      CAD_Pro_PlusX



  For Each row As DataGridViewRow In Me.TblPersonsDataGridView1.Rows
            If Not row.IsNewRow Then
                If Not row.Cells(64).Value.ToString.IsNullOrEmpty Then   >>>>> Here
                    If row.Cells(64).Value Is "Yes" Then
                        row.DefaultCellStyle.BackColor = Color.Red
                    End If
                End If
            End If
        Next
0
Jorge PaulinoIT Pro/DeveloperCommented:
Hum, try this way, but I will test meanwhile
If Not String.IsNullOrEmpty(row.Cells(64).Value) Then    
 
0
wiswalldAuthor Commented:
Conversion from type 'DBNull' to type 'String' is not valid.
0
Jorge PaulinoIT Pro/DeveloperCommented:
This way should work
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                If Not row.Cells(1).Value Is DBNull.Value Then
                    If row.Cells(1).Value Is "Yes" Then
                        row.DefaultCellStyle.BackColor = Color.Red
                    End If
                End If
            End If
        Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wiswalldAuthor Commented:
I got it with

 For Each row As DataGridViewRow In Me.TblPersonsDataGridView1.Rows
            If Not row.IsNewRow Then
                If Not IsDBNull(row.Cells(64).Value) Then
                    If row.Cells(64).Value = "Yes" Then
                        row.DefaultCellStyle.BackColor = Color.Red
                    End If
                End If
            End If
        Next

My datagridview is on a tabcontrol and I need this to execute somehow. I tried the form load, tabpage enter. What should I use to execute. I have it on a button right now.
0
wiswalldAuthor Commented:
I also need to take into consideration when I change records on the form.
0
Jorge PaulinoIT Pro/DeveloperCommented:
You have tried on form load and what's the problem ?
0
wiswalldAuthor Commented:
Nothing on the form load. Worked in the datagridview paint.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Maybe I'm not understanding your goal.
You want to fill the rows with red color that has on column 64 the work "Yes", right ?
If you you have to do two things:
1st - After fill the datagridview you run that code to paint the rows
2nd - On the event CellValueChanged you check if the column it the 64 (e.ColumnIndex) and if the cell has the work "Yes" you paint only that row.
0
wiswalldAuthor Commented:
This is what I have now and it works. I have a datagridview on a form with a contextmenustrip. The menu has one command that will set cell 64 to yes. When this happens I want that row to be red. This works. I also need the ability to change records and highlight that row red if the cell (64) is yes. I used the paint event for the datagridview and it worked. Here is the code. You pointed me in the right direction.

Private Sub TblPersonsDataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TblPersonsDataGridView1.Paint
        For Each row As DataGridViewRow In Me.TblPersonsDataGridView1.Rows
            If Not row.IsNewRow Then
                If Not IsDBNull(row.Cells(64).Value) Then
                    If row.Cells(64).Value = "Yes" Then
                        row.DefaultCellStyle.BackColor = Color.Red
                    End If
                End If
            End If
        Next
    End Sub



Same code runs when the contextmenustrip code runs.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, is working or you have any other problem ?
0
wiswalldAuthor Commented:
You put me on the right track
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.