Datagridview Checkbox Cell won't check
I have a Windows Form that contains a Datagridview control. This control has 2 columns, one is a DataGridViewCheckBoxCell (which is checked by default) and the other is a TextBox cell. If DGV contains multiple rows, I can check/uncheck the CheckBox for all rows except the first row (0 index). I don't understand why this happens for the first row only. Below is the code I have for this DGV.
Code:
Private Sub dgvSN_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvSN.CellMouseUp
'Debug.Print("dgvSN_CellMouseUp")
Try
EH.ErrorMessage = String.Empty
If e.RowIndex > -1 And e.ColumnIndex = 0 Then
Dim cboSelected As DataGridViewCheckBoxCell = CType(dgvSN.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)
If CBool(cboSelected.Value) = True Then
cboSelected.Value = False 'Even when this line is executed, the checkbox WON'T uncheck for the very first row
Else
cboSelected.Value = True
End If
End If
Catch ex As Exception
EH.ErrorMessage = "frmCertificates/dgvSN_CellMouseUp() - " & ex.Message & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
End Sub
Re: Datagridview Checkbox Cell won't check
What exactly is that code supposed to be achieving from the user's perspective?
Re: Datagridview Checkbox Cell won't check
After commenting out that code....NOTHING! It does absolutely nothing. However, I still can't check/uncheck the very first line. The rest of the lines...no problem!
I did notice something though. If the first row was checked, which it is by default, and I tried to uncheck it...it would do anything. However, as soon as I checked/unchecked another CB, the first one would become unchecked. Then, I would try and check the first CB again and nothing happened until I checked/unchecked another one. Only then would the first CB become checked.
Re: Datagridview Checkbox Cell won't check
That wasn't the question. The question was: From the user's point of view, what is SUPPOSED to happen? I'm going to go out on a limb and say that what is supposed to happen is that the checkbox should be able to check/uncheck.
If the field is a DataGridViewCheckBoxCell why are you handling it?
And of course I'm going to go with the stupid question and ask, have you set breakpoints and stepped through the code to make sure it's running the way you think it should be?
-tg
Re: Datagridview Checkbox Cell won't check
I have code for what is to happen when the checkbox is CHECKED. No point in putting it in until I figure out why the first row checkbox won't check/uncheck when clicked on. I have stepped through the logic I provide and it should check/uncheck just like the rest of the rows. It's the first row only that is acting this way.
Re: Datagridview Checkbox Cell won't check
Quote:
Originally Posted by
techgnome
If the field is a DataGridViewCheckBoxCell why are you handling it?
Exactly. You don't have to do anything to be able to check and uncheck the box in a DataGridViewCheckBoxCell so, if it doesn't work that way then either something is corrupt or else some other code is interfering with the default behaviour. If we know what that code is supposed to be doing then we can hopefully determine why it's having the undesired effect. If the code originally posted can be removed without effect and the unexpected behaviour persists then it must be some other grid event handler that's causing it. I'd be inclined to comment out all such methods and then add them back in one by one until the behaviour returns.
Re: Datagridview Checkbox Cell won't check
When the code is working as it should, it will make a call to the database and retrieve data to populate another DGV. I guess my next question would be....which Event should I use in order to facilitate this data retrieval?
Re: Datagridview Checkbox Cell won't check
Check out my MSDN code sample that may very well assist you with your current task. There are no direct answer here but information via projects to learn from.
Basics to advance working with DataGridView Checkbox columns
Re: Datagridview Checkbox Cell won't check
Well, mouse up isn't what I would have used, but I think I found the issue...
I used the CellContentClick ... I think it's a bit more stable than mouseup...
but what's happening is the cell is in edit mode, so it doesn't initially hold the new value... you have to stop editing in order for the value to commit.
In an empty project, add a DataGridView, add two cols, the first being a checked cell and the second being text, and add this codE:
Code:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = 0 Then
Dim cboSelected As DataGridViewCheckBoxCell = CType(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)
MessageBox.Show(cboSelected.Value)
End If
End Sub
when you run it, and click the first check cell... you'll get a blank messagebox, but the cell will look checked... dimiss the messgebox, click the checkbox again. The check mark will clear but the messagebox will read True.
Now make this one line change:
Code:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = 0 Then
Dim cboSelected As DataGridViewCheckBoxCell = CType(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)
DataGridView1.EndEdit()
MessageBox.Show(cboSelected.Value)
End If
End Sub
Run the same test... now you'll see the True/False in synch with the checkmark in the UI.
-tg
Re: Datagridview Checkbox Cell won't check
I have attached a mp4 video showing my actions. Currently, I have NO Events coded for the DGV...
Re: Datagridview Checkbox Cell won't check
Quote:
Originally Posted by
blakemckenna
I have code for what is to happen when the checkbox is CHECKED. ...
Quote:
Originally Posted by
blakemckenna
When the code is working as it should, it will make a call to the database and retrieve data to populate another DGV. I guess my next question would be....which Event should I use in order to facilitate this data retrieval?
The DGV's events can be a bit confusing. I believe what you want is the CurrentCellDirtyStateChanged event.
VB.Net Code:
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
Dim cell As DataGridViewCheckBoxCell = TryCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)
If cell IsNot Nothing AndAlso cell.ColumnIndex = 0 Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
If CBool(cell.Value) Then Beep()
End If
End Sub
Re: Datagridview Checkbox Cell won't check
TnTinMN,
Below is the code that I am using and still no changes. I don't understand why this is happening. I've also attached a video.
Code:
Private Sub dgvSN_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgvSN.CurrentCellDirtyStateChanged
'Debug.Print("dgvSN_CurrentCellDirtyStateChanged")
Try
Dim cell As DataGridViewCheckBoxCell = TryCast(dgvSN.CurrentCell, DataGridViewCheckBoxCell)
If cell IsNot Nothing AndAlso cell.ColumnIndex = 0 Then
dgvSN.CommitEdit(DataGridViewDataErrorContexts.Commit)
If CBool(cell.Value) <> False Then
cell.Value = False
Else
cell.Value = True
End If
End If
Catch ex As Exception
EH.ErrorMessage = "frmCertificates/dgvSN_CurrentCellDirtyStateChanged() - " & ex.Message & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
End Sub
Re: Datagridview Checkbox Cell won't check
Here's one of my bookmarked link on DataGridViewCheckBoxCell event.
VBForum DataGridViewCell question
Re: Datagridview Checkbox Cell won't check
What is the point the purpose of this code?
Quote:
Originally Posted by
blakemckenna
Code:
If CBool(cell.Value) <> False Then
cell.Value = False
Else
cell.Value = True
End If
If your goal is to prevent changes, then that code might have relevance if the changes were committed by executing a second CommitEdit(DataGridViewDataErrorContexts.Commit) statement.
The CurrentCellDirtyStateChanged event will fire after as the cell value changes. The CommitEdit(DataGridViewDataErrorContexts.Commit) method will tell the DGV to accept the change as the current value.
If your goal is to respond immediately to changes based on the cell value, then:
Code:
If CBool(cell.Value) Then ' note that you should not test if a boolean = True/False, i.e.: CBool(cell.Value) = True
' execute code for when true
Else
' execute code for when false
End If
Do not try to set the cell value in code.
Re: Datagridview Checkbox Cell won't check
unless your intent is to change the checkmark setting, stop doing so... I think that's part of the problem. you're handling an event and then changing it and wondering why it doesn't work. you would actually be better off with a message box or some other action that isn't mucking about with the data you're trying to process. at the moment the code will uncheck a checked box... or check the box you just unchecked... the net effect is nothing... no change... or a checkbox that is basically locked & readonly. if that's the intent, there's better ways of handling that. But your previous comments about only wanting to do an action if it is checked says that's not what you're after... OK... fine... then DO something when it is checked but stop mucking with the data and setting which is undoing what hte check box wants to do naturally. display a message box. call your calculations you want to do, write to the debug console windopw, DO SOMETHING but STOP unsetting/setting the check box.
-tg
Re: Datagridview Checkbox Cell won't check
Well...I found a direct correlation to the problem. Initially, I had the ColumnHeadersVisible set to False. When I changed it back to True (Default setting), it worked! I was able to check/uncheck the first row. Not sure if that is a bug or not...seems to me it is. My work-around was to set the ColumnHeaderDefaultCellStyle FontSize to 1 and clear out the Column.HeaderText property. Everything works as it should with just a small hint of the Column Headers.