How to commit a cell containg a checkboc immediately

johnpapa

Member
Joined
Aug 10, 2013
Messages
23
Programming Experience
10+
In a DGV I have a field which is a checkbox. As an example let's say that there are 10 rows in the DGV. If I click on the checkbox in row, say 3, in order to set it to True, I would like to set all other checkboxes to False, ie checkboxes in rows 1,2 and 4 to 10, using an Update statement which changes the SQL Server.

When I click on the checkbox in row 3 it changes to True and the specific cell and row 3 become dirty and the Update does not seem to work. If I move to different row prior to Updating then there is no problem.

Is there a way to commit the cell contents and change the row status to not dirty without having to click to a different row. I tried dgv.endEdit with no success.

John
 
Not surprisingly, the MSDN documentation for the DataGridViewCheckBoxCell class addresses this issue specifically. You should ALWAYS read the relevant documentation first.
 
Thanks for the reply, I have been trying since you pointed out the doc, which says

Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

Just repeating that when I click on a checkbox, I basically want the value to be stored in the checkbox and the row to become not-dirty (as if I clicked on a different row.)

Calling the CommitEdit does not remove the pencil on the left of the row, indicating that the row is still dirty.

John
 
Calling the CommitEdit does not remove the pencil on the left of the row, indicating that the row is still dirty.
Then I think you're handling the wrong event, or have something wrong in the event handler. You should call CommitEdit in CurrentCellDirtyStateChanged event handler ('if the current cell is a check box cell'). Doing this will commit the edit and remove the 'dirty' pencil mark.
 
Thanks for the reply, I used

dgv.CommitEdit(True)
in
Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged

I got the attached message (for some reason I could not attach .jpg or .png)

The pencil is still there.

John

 

Attachments

  • Checkbox.pdf
    134.3 KB · Views: 64
John,

The following works fine, but the .Commit gets executed twice when I click on a checkbox. Is this correct?
    Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged
        Try
            dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        Finally
        End Try
    End Sub

 
Last edited by a moderator:
John,

The following works fine, but the .Commit gets executed twice when I click on a checkbox. Is this correct?
    Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged
        Try
            dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        Finally
        End Try
    End Sub


It depends what you mean by correct. The event is going to be raised twice because the cell becomes dirty when you edit it and then becomes not dirty when you commit the change. If you don't want to commit anything the second time then don't. If you only want to commit a change when the cell becomes dirty then you have to actually check whether the cell is dirty.

By the way, I fixed your code formatting.
tags are for quotes, not code.
 
The problem of immediately committing a checkbox value following a click was solved by using

dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)

in

Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged

as pointed out above. Many thanks for all your help
 
Back
Top