Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Change datagridview row color based on cell value

Posted on 2008-11-08
15
Medium Priority
?
4,276 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:wiswalld
  • 8
  • 7
15 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22913994
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
 
LVL 13

Author Comment

by:wiswalld
ID: 22932271
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22932331
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Author Comment

by:wiswalld
ID: 22933286
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22933427
Hum, try this way, but I will test meanwhile
If Not String.IsNullOrEmpty(row.Cells(64).Value) Then    
 
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22933462
Conversion from type 'DBNull' to type 'String' is not valid.
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 750 total points
ID: 22933527
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
 
LVL 13

Author Comment

by:wiswalld
ID: 22939249
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
 
LVL 13

Author Comment

by:wiswalld
ID: 22939342
I also need to take into consideration when I change records on the form.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22944498
You have tried on form load and what's the problem ?
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22949227
Nothing on the form load. Worked in the datagridview paint.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22950053
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
 
LVL 13

Author Comment

by:wiswalld
ID: 22950405
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22953119
Ok, is working or you have any other problem ?
0
 
LVL 13

Author Closing Comment

by:wiswalld
ID: 31514714
You put me on the right track
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

577 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