8

I have checkboxes in one of my columns in DataGridView.

And now I have a problem: When I click once on Checkbox it changes but only visualy, in code its value is still set to false. But if I click on Checkbox and then anywhere else on my datagridview (or change its value manually in code on true) it changes its value on true.

How can I force my checkbox to change value after one click? (it's annoying that checking checkbox actually does not check it).

Thanks for any help.

Peter
  • 912
  • 3
  • 11
  • 29
  • The changes are applied when the control loses the focus. Are you using autogenerated columns or explicitly created ones? It is the same behavior as when editing text field: the underlying object will not change on each typed letter, it will change at the end, when the focus is changed. This is by design. – Zdeslav Vojkovic Apr 15 '13 at 12:02
  • The column is autogenerated. So is there any event that will work in this situation? – Peter Apr 15 '13 at 12:06
  • 1
    `CellContentClick` if you want to handle it explicitly – Zdeslav Vojkovic Apr 15 '13 at 12:06
  • I've upvoted you back to zero. I see nothing wrong with your question. – Joel Aug 19 '14 at 16:34

2 Answers2

10

Changes to underlying data source are applied when the controls lose the focus. You can handle it explicitly in CellContentClick event.

Please read the linked documentation thoroughly, as it describes similar scenarios, and discusses different type of grid cells.

Found also this. Exactly the same problem.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • I got the same issue. I just add Private Sub EndEditMode(sender As System.Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged If dgv.IsCurrentCellDirty Then dgv.CommitEdit(DataGridViewDataErrorContexts.Commit) End If End Sub `` And it works... – 8oris Feb 02 '22 at 15:16
2

Lets assume like you have a form with name Form1

A DataGridView in it name dgv

A CheckBoxColumn in it with ColumnIndex = 5

public Form1()
{
    InitializeComponent();
    dgv.CellContentClick += dgv_CellContentClick;
}

Commit the change for datagrid within the event for that specific column

private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 5) dgv.EndEdit();
}
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45