Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox in DataGridView does not change its value after checking it

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.

like image 645
Peter Avatar asked Apr 15 '13 11:04

Peter


2 Answers

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.

like image 184
Zdeslav Vojkovic Avatar answered Oct 24 '22 19:10

Zdeslav Vojkovic


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();
}
like image 2
Abdul Saleem Avatar answered Oct 24 '22 17:10

Abdul Saleem