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

Highlite Cell in Datagridview

Highlite Cell in Datagridview

(OP)
I'm trying to highlite a cell of a gridview based on a certain value.

DB field named Monday, DB field Named M_H

if M_H = "Y" then I want to highlite the Monday field in the grid only for that row. I'm trying to tweak the below code to only go to the specific cell in that row where cell(16) is = to "Y" the below code highlites the entire column which makes sense based on the code, how do you specify a certain cell ?

Thanks in advance

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

RE: Highlite Cell in Datagridview


DefaultCellStyle affects the entire row. Also, you need to specify the cell you want colored, not the column.

CODE

For i As Integer = 0 To dg.RowCount - 1
    If dg.Rows(i).Cells(16).Value.ToString = "Y" Then
        dg.Rows(i).Cells(1).Style.BackColor = Color.Yellow
    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: Highlite Cell in Datagridview

(OP)
perfect, thank you

RE: Highlite Cell in Datagridview

(OP)
ran into a little issue .. what if I want to check more then one cell value.

I would like to check cells 16 - 20 and highlite cells 1, 2, 3 ,4, 5 if cells 16-20.value = "Y"

I've tried to combine those cells in the above code but the grid just freezes...

Thanks

RE: Highlite Cell in Datagridview


Is it something like:

If Cell 16 = "Y" then color Cell 1
If Cell 17 = "Y" then color Cell 2
and so forth..

In this case, you would just reuse the code above, changing the cells to be checked:

CODE

For i As Integer = 0 To dg.RowCount - 1
        If dg.Rows(i).Cells(16).Value.ToString = "Y" Then
            dg.Rows(i).Cells(1).Style.BackColor = Color.Yellow
        End If

        If dg.Rows(i).Cells(17).Value.ToString = "Y" Then
            dg.Rows(i).Cells(2).Style.BackColor = Color.Yellow
        End If

        'and so forth

    Next 

If this isn't what you needed, post the code you have now.

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: Highlite Cell in Datagridview

(OP)
Thanks

My problem was I had the code running from the grid cell painting event. I changed it to the cell formatting event and it now works.

thanks again

RE: Highlite Cell in Datagridview

(OP)
Everything is working great except if the first cell in a row is marked as "Y" then it highlites the entire column not just the one cell. if the second and below cell is marked as "Y" the code works fine.

I'm using

For i As Integer = 0 To dg.RowCount - 1
If dg.Rows(i).Cells(16).Value.ToString = "Y" Then
dg.Rows(i).Cells(1).Style.BackColor = Color.Yellow
End If

Next

RE: Highlite Cell in Datagridview


Well, I can't recreate the issue. Could you show all of the code in the CellFormatting event handler?

One other thing, since you are using the CellFormatting event to run this code you do not need to do the loop through the grid. You can just do this:

CODE

If DataGridView2.Rows(e.RowIndex).Cells(1).Value IsNot Nothing Then
    If DataGridView2.Rows(e.RowIndex).Cells(1).Value.ToString = "Y" Then
        DataGridView2.Rows(e.RowIndex).Cells(0).Style.BackColor = Color.Yellow
    End If
End If 

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!

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