INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Change Color of row in DatagridView based on Value

Change Color of row in DatagridView based on Value

(OP)
I've been searching around and really can't seem to get this to work.

I have a datagridview that shows records and I want any row in the grid to change color based on a certain condition.

basically if dg.Columns(9).Value.ToString = "Y" Then

e.CellStyle.BackColor = Color.Red

I;m not sure how to loop through the grid and check that column.

any help would be appreciated.

Thanks

RE: Change Color of row in DatagridView based on Value


Try something like this:

CODE

For i As Integer = 0 To dg.RowCount - 1
    If dg.Rows(i).Cells(9).Value.ToString = "Y" Then
        dg.Rows(i).DefaultCellStyle.BackColor = Color.Red
    End If
Next 

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: Change Color of row in DatagridView based on Value

(OP)
Thanks,

I tried that and it's like the code doesn't fire. I've tried running it in the form load and also grid cell formatting event and no luck. Can't figure out why the code doesn't fire

RE: Change Color of row in DatagridView based on Value

(OP)
I'm now getting this error....

conversion from string "Datagridview textbox cell" {Column to type Boolean is not valid

RE: Change Color of row in DatagridView based on Value

(OP)
okay, I got it to work

Try

For i As Integer = 0 To dg.RowCount - 1
If dg.Rows(i).Cells(12).Value = "Y" Then
dg.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Next

Catch ex As Exception

End Try

but it skips the first row in the grid

RE: Change Color of row in DatagridView based on Value

(OP)
I take that back.... it's working but what seems to be happening is that the grid is going back to the original formatting colors for the first row.

RE: Change Color of row in DatagridView based on Value

(OP)
nevermind....got it.

thanks for your help

RE: Change Color of row in DatagridView based on Value

Hi There, please can you explain what you find when you solve your problem, rather than just say "never mind, I got it". It would help others to fix the problem in case they are stuck too.

Thanks.

RE: Change Color of row in DatagridView based on Value

(OP)
the grid had focus on the first row and I have the grid alternating colors. that's what the problem was.

RE: Change Color of row in DatagridView based on Value

Ah- good one- thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close