Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Color of row in DatagridView based on Value 1

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
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

 

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!
 
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

 
I'm now getting this error....

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

 
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

 
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.

 
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.

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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top