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!

Highlite Cell in Datagridview 1

Status
Not open for further replies.

dvannoy

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

 

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
        [red]dg.Rows(i).Cells(1).Style.BackColor = Color.Yellow[/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!
 
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

 

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

 
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

 

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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top