Reputation: 651
Situation:
I'm developing a winforms application with C# in VS2013.
I have a datagridview bound to a MySQL table through a DataAdapter. Row validation and database updating takes place whenever a user clicks away from an edited row to a new row. Row validation is handled in RowValidating and dataadapter.Update is handled in RowValidated.
Issue:
Where validation errors occur in RowValidating then I can use Cancel = true to stop the update and keep the row in error as the current selection. However validation can also fail when the database gets updated (e.g. where database constraints or concurrency prevent update). I can trap these but, as I'm now in RowValidated, Cancel = true is no longer available. As a result I end up with the old row flagged as an error but selection has moved on to the new row.
Question:
Is there some way of abandoning further processing and in particular the move to a new row once things have progressed as far as an Update in RowValidated? Put another way, is there some analogue of Cancel = true in RowValidated?
Upvotes: 2
Views: 296
Reputation: 651
Found a way forward with this at https://msdn.microsoft.com/en-us/library/ms229603.aspx.
It says:
If you have custom validation that takes place after the Validating event, it will not affect the data binding. For example, if you have code in a Validated event that attempts to cancel the data binding, the data binding will still occur. In this case, to perform validation in the Validated event, change the control's Data Source Update Mode property (under (Databindings)(Advanced)) from OnValidation to Never, and add Control.DataBindings[""].WriteValue() to your validation code.
Upvotes: 1