C datagridview cellvalidating cancel

Posted by / 25-Mar-2017 21:34

It turns out that this is trickier than it might seem and doesn’t work great out of the box from .NET, but, not to fear, there are several easy work-arounds that will get the trick done.What if someone is able to make a selection without a click event?

_event which executes my function changing the value of the cell to 0) --focus is now on the cell to the left of original cell, but the original is still blank- Yet value is 0.

I’d rather code to do exactly what it says it’s doing. This has the added bonus of not firing when you have clicked outside of the checkbox area, but still falls perhaps under the same category of not being entirely clear to someone unfamiliar with this issue why this particular event should force the grid to exit edit mode.

After looking into the problem at some length, MSDN actually seems to offer the best solution right on their Cell Content Click event page. I like that it tells us very specifically what has happened to the grid at the point in time when the event is raised and handled.

When you click a Check Box in a Data Grid View Check Box Column, the check marker will update immediately, but Cell Value Changed event on the Data Grid View will not fire until the user happens to click elsewhere and the cell has lost focus. Well, the Data Grid View thinks it’s a little preemptive to go declaring that the cell value has changed while you are still selected on it.

This makes more sense when we think about a Text Box column.

c datagridview cellvalidating cancel-68c datagridview cellvalidating cancel-75c datagridview cellvalidating cancel-75

It’s important to note that this event will get called twice, once on changing the state of the cell to dirty (before committing changes) and once when changing the cell state back to ‘clean’ (after the changes have been committed).