Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.)
VB
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
            'Header row do nothing
        Else
            'Not header row - make sure just one row is selected
            If dgvCommingledProd.SelectedRows.Count = 1 Then
                'Determine which column was clicked
                If e.ColumnIndex = dgvCommingledProd.Columns(colAction.Name).Index Then
                    'Check or uncheck the box
                    dgvCommingledProd.SelectedRows(0).Cells("colAction").Value = Not dgvCommingledProd.SelectedRows(0).Cells("colAction").Value

                    'If it's not the row header...
                    If e.RowIndex <> -1 Then
                        With dgvCommingledProd.Rows(e.RowIndex)
                            If .Cells(colAction.Name).Value = True Then
                                'User checked the field, set focus to the production column
                                .Cells(conststrRecordedProduction).ReadOnly = False
                                dgvCommingledProd.CurrentCell = dgvCommingledProd.Rows(e.RowIndex).Cells(conststrRecordedProduction)
                            Else
                                'User unchecked the field, clear the value
                                .Cells(conststrRecordedProduction).ReadOnly = True
                                .Cells(conststrRecordedProduction).Value = DBNull.Value
                            End If

                            'Set the cell's color
                            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

VB
Private Sub SetProductionCellStyle(ByRef dgvc As DataGridViewCell)
    If dgvc.ReadOnly Then
        'It's read only, grey it out
        dgvc.Style.BackColor = Color.LightGray
        dgvc.Style.SelectionBackColor = Color.LightGray
        dgvc.Style.ForeColor = Color.Black
        dgvc.Style.SelectionForeColor = Color.Black
    Else
        'Use white & black
        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.
VB
'Commented out this line:
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?
Posted
Updated 14-May-12 8:27am
v2

I think the reason may be that when the CheckBox cell is clicked it will be in DirtyState. When somewhere else mouse is clicked the data gets committed and the color changes. To force the commit manually the CurrentCellDirtyStateChanged can be handled as explained here.

DataGridView.CurrentCellDirtyStateChanged Event[^]

Please give it a try. I think it may be helpful.
 
Share this answer
 
Comments
Kschuler 14-May-12 14:42pm    
I tried the code sample in this link...instead of using it to enable/disable a button I set the colors on my textboxcell. And it still isn't doing what I want, so I'm either not understanding how to use it correctly or it's just not what I am looking for. Using that code it no longer changes to light gray when I uncheck the box, but if I click somewhere else THEN it changes to light gray. I think my problem really has to do with my setting the CurrentCell when the user checks the box. If you have any more ideas please let me know.
VJ Reddy 14-May-12 15:00pm    
As it is stated in the Update to the question that when the assignment to the CurrentCell property is commented out it is working as expected, then the possibility could be that DataGridView may be triggering some event which is obstructing the required action. To overcome this the BeginInit and EndInit method given here
http://msdn.microsoft.com/en-us/library/bb341552(v=vs.110).aspx
or BeginEdit and EndEdit methods given here
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.beginedit
may be helpful.
Kschuler 14-May-12 15:37pm    
That didn't help me either. Thanks for the suggestion though. I ended up moving the current cell code to after the set color code, and it worked. So I guess it was a timing thing. Again, thanks for taking the time to consider my problem!
VJ Reddy 14-May-12 20:39pm    
Thank you for the consideration.
Oi. This one was quite annoying. I solved the issue by moving the code that sets the current cell to execute AFTER the code that sets the color. And all works fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900