Sunil
Sunil

Reputation: 175

Unchecking the user checked checkbox in the datagridview cell

I have a column in my DataGridView which has the DataGridViewCheckBoxColumn as column template. When user checks the checkbox, I need to uncheck it programmatically if it fails some conditions. I have done this, but this uncheck logic only takes effect after I change the focus from the cell manually. I tried changing the focus programmatically but didn't work. Is there any solution for this?

Upvotes: 1

Views: 5125

Answers (3)

Sorad
Sorad

Reputation: 21

Recently I had similar problem. Setting Cell Value on false or working with TrueValue and FalseValue (for DataGridViewCheckBoxCell) failed in my case.

Finally i manage to achieve what I wanted (unchecking if some condition fails) with simple DataGridView method:

dataGridView1.CancelEdit()

Hope it helps.

Upvotes: 2

Sunil
Sunil

Reputation: 175

I found the answer with little code changes. I used the cancel edit function of datagridview. For checkbox value change to be fired previously i was using this code inside CurrentCellDirtyStateChanged event.

CommitEdit(DataGridViewDataErrorContexts.Commit) 

Now i changed it to.

CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange) 

And called the

CancelEdit()

to roll back the changes.

Upvotes: 2

Ria
Ria

Reputation: 10347

Just set value of your DataGridViewCell.Value property to false (or true):

((DataGridViewCheckBoxCell)row.Cells[CheckBoxColumn.Index]).value = false;

Upvotes: 1

Related Questions