As part of my validation process I need to catch DataRows that do not satisfy the constraints imposed upon the DataTable they are being stored in. If they do not satisfy the constraints they cannot be added to the DataSet and therefore I will not be able to delete them in the RowValidated event.
What I am looking for is some way to dismiss a row that fails my validation criteria (and possible the constraints) prior to the row being added to the DataSet, which could cause a run time error.
Ok, the datagridview will already have thrown an error before the
RowValidating event if the data doesn't match the column type in the datasource.
What you can do is subscribe to the
DataError event (couldn't see it in the properties, so may need to be done in code.).
From here, you can check the exception etc and display a warning\do stuff before calling the DataGridView.Rows.RemoveAt to remove the row from the datagridview in this event. If you had more stringent requirements like this column needs to be below 10 etc, then you could check it in the
RowValidated event and move it from the datasource.
Now there would be a problem with this method if the data was not allowed to be written to the datasource unless it met all your custom (value <10) conditions. In this case you can stop it by calling the cancel method (e.cancel = true) in the RowValidating event which stops the data from being added to the datasource.
This doesn't remove it from the gridview however. One way of removing this I suppose would be to rebind the datasource at this point.
There is one thing I noticed when I was doing this. The
rowvalidating event is called when you close the form that the datagridview is on. If you have cancel=true here when this happens, it will not allow you to close the form. Something to be aware of. I didn't look any further into this.
Just to let you know, I'm assuming that your using a DataGridView in a winform cause the web gridview doesn't have these events.