Avatar of Richard Kreidl
Richard Kreidl
Flag for United States of America asked on

Change the color of a row in datagrid when certain column is not blank

I have the following code that 's giving me the following error when I run it. I attached a screen print of the errror.

As you can see in the screen print it says: Column Named Vacation_On cannot be found.

Which doesn't make sense, because the column exists in my Access database and I can see it in the datagridview.

Any ideas what's causing the error??

thanks


For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If row.Cells("Vacation_On").Value Is DBNull.Value Then
                row.DefaultCellStyle.BackColor = Color.White
            Else
                row.DefaultCellStyle.BackColor = Color.Aqua
            End If
 Next

Open in new window

10-10-2009-10-45-17-AM.png
Visual Basic.NET

Avatar of undefined
Last Comment
jppinto

8/22/2022 - Mon
jppinto

Try like this. On the row.Cells(1).Value  change the 1 for the number of the column that represents Vacation_On and the code should work.

jppinto
For Each row As DataGridViewRow In Me.DataGridView1.Rows
   If row.Cells(1).Value Is DBNull.Value Then
      row.DefaultCellStyle.BackColor = Color.White
   Else
      row.DefaultCellStyle.BackColor = Color.Aqua
   End If
Next

Open in new window

Richard Kreidl

ASKER
It's not working, more rows are changing colors than are supposed to.
jppinto

But at least you don't get the error anymore, right?

Did you putted the correct index of the column you want to check is the cell value is blank? In my example I assumed that Vacation_On is the the second column (index 1). The code is correct, maybe the column that you're checking is not the correct one...


jppinto
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Richard Kreidl

ASKER
I tried changing the index in the code several times and I'm still not getting the results I'm expecting.

The Vacation_On column is blank, so all the rows should be white, but they're not.
jppinto

Can you show a print screen of your datagridview?
Richard Kreidl

ASKER
added screen print
10-11-2009-5-36-49-AM.png
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jppinto

What is the index that you're using? 7?
Richard Kreidl

ASKER
I tried 7 and 8, it doesn't work with either one.
jppinto

This should color the rows where the column Vacation_on is null in white (index 8) and color the rows where Vacation_On is filled in Aqua. Is that what you want to do? You have to make sure that, when there is no date in Vacation_On field on your database, that the field is null or empty and doesn't have like a 0 on it.

I've tryed the code on and this is the result that I got (please see attached picture). To get this result I've used this code:

For Each row As DataGridViewRow In Me.grdRelatorio.Rows
            If Not row.IsNewRow Then
                  If row.Cells(4).Value Is DBNull.Value Then
                    row.DefaultCellStyle.BackColor = Color.White
                Else
                    row.DefaultCellStyle.BackColor = Color.Aqua
                End If
            End If
Next

When the 4th column is empty, the field on the database is empty!

jppinto
For Each row As DataGridViewRow In Me.grdRelatorio.Rows
   If Not row.IsNewRow Then
      If row.Cells(8).Value Is DBNull.Value Then
         row.DefaultCellStyle.BackColor = Color.White
      Else
         row.DefaultCellStyle.BackColor = Color.Aqua
      End If
   End If
Next

Open in new window

DataGridView.jpg
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Richard Kreidl

ASKER
No luck, the Vacation_On column is empty in the whole database. I should have one color, but I don't.
jppinto

I'm getting out of options here...

Can you post an sample of your data on your table, the definition of your table and a screen shot of the datagridview with data after running my last code to see how it looks?

I've answered another question sometime ago about this and I was able to solve the problem. Take a look at this to see if you can see anything more that can help you because I can't figure it out what the problem is:

https://www.experts-exchange.com/questions/24332056/changing-the-back-colour-of-the-datagridview-if-the-column-contains-something.html

jppinto
ASKER CERTIFIED SOLUTION
jppinto

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.
Richard Kreidl

ASKER
Yes, that did it!!!

Thanks!!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Richard Kreidl

ASKER
Thanks again!!!
jppinto

Uff.... I was getting tyred of thinking on this one! Glad I could help. Regards.