views:

219

answers:

2

As far as I can tell, the best way to do this is do it in the DataTable.RowChanging event. But what if I want to cancel the action? There is no EventArgs.Cancel option...

+1  A: 

From the DataTable.RowChanging event handler, you can throw an exception to cancel the change. The exception will be thrown to the piece of code making the change where you can handle it appropriately.

TheSoftwareJedi
Hmm. That makes sense - but isn't throwing an exception a costly operation?
Camel
costly compared to just manipulating a local variable - sure... but are you planning on this happening hundreds of times per second?
TheSoftwareJedi
A: 

In the case of the DataGridView, if you throw an exception in DataTable.RowChanging you can handle it in DataGridView.OnError. Setting EventArgs.Cancel to true preserves data entry.

Camel