Question DataGridView cell painting

Misko

Member
Joined
Jan 5, 2009
Messages
9
Programming Experience
Beginner
Dear Experts,

I have a datagridview having multiple rows with the following structure:

|ID| Name| Address | StatusCode |

I would like to paint an ID cell if the value in the corresponding StatusCode cell is 1.
I only managed to paint the StatusCode cell using the following code:

Private Sub dgCorpResults_CurrentCellChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgCorpResults.CellFormatting
' Changes how cells are displayed depending on their columns and values.
' Set the background to red for values 1 in the StatusCode column.
If dgCorpResults.Columns(e.ColumnIndex).Name.Equals("StatusCode") Then
If CInt(e.Value) = 1 Then
e.CellStyle.BackColor = Color.Red
e.CellStyle.SelectionBackColor = Color.DarkRed
End If
End If
End Sub

Thanks for helping me out!
Misko
 
Try this

VB.NET:
Private Sub dgCorpResults_CurrentCellChanged(ByVal sender As Object, _ 
        ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgCorpResults.CellFormatting 
        ' Changes how cells are displayed depending on their columns and values. 
        ' Set the background to red for values 1 in the StatusCode column. 
        If dgCorpResults.Columns(e.ColumnIndex).Name.Equals("ID") Then 
            If CInt(dgCorpResults.Rows(e.RowIndex).Cells("StatusCode").Value) = 1 Then 
                e.CellStyle.BackColor = Color.Red 
                e.CellStyle.SelectionBackColor = Color.DarkRed 
            End If 
        End If 
End Sub
 
Thanks a lot.
In the meantime, I have tried this:

Private Sub dgCorpResults_CurrentCellChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgCorpResults.CellFormatting
' Changes how cells are displayed depending on their columns and values.
' Set the background to red for values 1 in the StatusCode column.
If dgCorpResults.Columns(e.ColumnIndex).Name.Equals("StatusCode") Then
If CInt(e.Value) = 1 Then
dgCorpResults.Rows(e.RowIndex).Cells(0).Style.BackColor = Color.Red
End If
End If
End Sub

It works when "StatusCode" column is visible in the DataGridView.

Now, I am trying to do it in a different way in order to get my ID cell painted but "StatusCode" column invisible...

Thanks for any further help!
 

Latest posts

Back
Top