Question Cell painting in DataGridView

Misko

Member
Joined
Jan 5, 2009
Messages
9
Programming Experience
Beginner
Hi all,

I have developed a method that would paint a cell in the DataGridView depending on the value contained in another cell of the same row. This is my code:

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("StatusCode") Then 
If CInt(e.Value) = 1 Then
dgCorpResults.Rows(e.RowIndex).Cells(0).Style.Back Color = Color.Red
End If 
End If 
End Sub

This code works only if the column "StatusCode" is visible in the DataGridView.

I would like to paint an "ID" cell ie. Cells(0) and not show the column "StatusCode" in the DataGridView. I was thinking of using the filter and select statement on data source.
Any ideas on how could I do it?

Many thanks!
 
VB.NET:
    Private Sub dgCorpResults_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvPanelSawSchedule.CellPainting
        If e.RowIndex < 0 OrElse e.ColumnIndex < 0 Then
            e.Handled = False
            Exit Sub
        End If

        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
 
I would like to paint an "ID" cell ie. Cells(0) and not show the column "StatusCode" in the DataGridView. I was thinking of using the filter and select statement on data source.
Any ideas on how could I do it?
!
What does this mean? If you want an ID on the datagrid view, then add it to the underlying data source. If you simply want a row number, try this: Automatically Adding Numbers to the Rows in a DataGridView
 
InertiaM understood my problem. I have tried the code you suggested but it didn't work :-(
I have tried also the following code, it still didn't work...

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("StatusCode").Visible = False Then
e.FormattingApplied = True
End If

If dgCorpResults.Columns(e.ColumnIndex).Name.Equals("StatusCode") Then 
If CInt(e.Value) = 1 Then
dgCorpResults.Rows(e.RowIndex).Cells(0).Style.Back Color = Color.Red
End If 
End If 
End Sub

For cjard : What I need is the following:
Display in a DataGridView data having multiple rows and several columns. The cells in my first column (ID) should be painted if the value in the column "StatusCode" of the same row is 1. The column "StatusCode" should not be visible in the DataGridView.

Again, when the column "StatusCode" is visible in the DataGridView, the code works. My problem is that it doesn't work when this column is invisible.

Is there a way to save formatted cells and then put the column "StatusCode" to invisible?

Many, many thanks!!!
 
It works fine for me. I take it you did put it in the CellPainting event as per my code?
 
Yes I did. The only thing I changed is in
VB.NET:
Private Sub dgCorpResults_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvPanelSawSchedule.CellPainting
Instead of
Handles dgvPanelSawSchedule.CellPainting
I put
Handles dgCorpResults.CellPainting
Maybe that's why it's not working.
What is dgvPanelSawSchedule? A panel?
 
Back
Top