I've been playing around with this fairly simple issue for quite a while and have decided I just need someone else to look at it. I have a DataGridView with a checkbox column in it. When the box is unchecked, I want a different cell in the row to be ReadOnly and the BackColor to be light gray. When it's checked, I want that cell to not be ReadOnly and the BackColor to be white. The ReadOnly bit is working fine, but I'm having trouble with changing the colors. When I uncheck a box, it turns light gray and ReadOnly just fine. But if I CHECK the box, it doesn't change back to white. I type something and then leave the cell and THEN it turns white. I tried a couple of different things to no avail and now I'm just to frutrated to think straight. Here is what I've got for code:
(
colAction
is the checkbox column,
conststrRecordedProduction
is a string constant with the name of the cell that I want to change color.)
Private Sub dgvCommingledProd_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvCommingledProd.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Left Then
If e.RowIndex = -1 Then
Else
If dgvCommingledProd.SelectedRows.Count = 1 Then
If e.ColumnIndex = dgvCommingledProd.Columns(colAction.Name).Index Then
dgvCommingledProd.SelectedRows(0).Cells("colAction").Value = Not dgvCommingledProd.SelectedRows(0).Cells("colAction").Value
If e.RowIndex <> -1 Then
With dgvCommingledProd.Rows(e.RowIndex)
If .Cells(colAction.Name).Value = True Then
.Cells(conststrRecordedProduction).ReadOnly = False
dgvCommingledProd.CurrentCell = dgvCommingledProd.Rows(e.RowIndex).Cells(conststrRecordedProduction)
Else
.Cells(conststrRecordedProduction).ReadOnly = True
.Cells(conststrRecordedProduction).Value = DBNull.Value
End If
Dim temp As DataGridViewCell = dgvCommingledProd.SelectedRows(0).Cells(conststrRecordedProduction)
SetProductionCellStyle(temp)
End With
End If
End If
End If
End If
End If
End Sub
Private Sub SetProductionCellStyle(ByRef dgvc As DataGridViewCell)
If dgvc.ReadOnly Then
dgvc.Style.BackColor = Color.LightGray
dgvc.Style.SelectionBackColor = Color.LightGray
dgvc.Style.ForeColor = Color.Black
dgvc.Style.SelectionForeColor = Color.Black
Else
dgvc.Style.BackColor = Color.White
dgvc.Style.SelectionBackColor = Color.White
dgvc.Style.ForeColor = Color.Black
dgvc.Style.SelectionForeColor = Color.Black
End If
End Sub
I can see when debugging that it's hitting the code to set the backcolor to white, but it's just not showing on the screen until later. Can anyone tell me what is happening and/or how to work around it?
*** UPDATE ***Okay, so if I comment out the line where I'm setting the current cell to the column when the box is checked in the action column, then the color change seems to work properly.
dgvCommingledProd.CurrentCell = dgvCommingledProd.Rows(e.RowIndex).Cells(conststrRecordedProduction)
So...maybe my question should be how can I set focus to the other column without interupting the color change?