1

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?

Sunil
  • 175
  • 2
  • 15

3 Answers3

2

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.

Sunil
  • 175
  • 2
  • 15
1

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

((DataGridViewCheckBoxCell)row.Cells[CheckBoxColumn.Index]).value = false;
Ria
  • 10,237
  • 3
  • 33
  • 60
  • no ria i had tried that but it will come effect only if I changed the focus from the cell – Sunil Nov 07 '12 at 05:34
1

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.

Sorad
  • 11
  • 1