I have been stuck with this for hours and hope somebody can shed some light on this. Say I have a DataGrid
control:
<DataGrid ItemsSource="{Binding Path=RealEstates, ValidatesOnExceptions=True}"
AutoGenerateColumns="True"
Name="grdData_RealEstate"
Margin="5,5,5,5"
Style="{StaticResource DataGridStyle}"
RowStyle="{StaticResource DataGridRowStyle}">
</DataGrid>
Which is bound to a propetry that exposes the RealEstates table of a DataSet
.
Now when the user deletes a row from this table I would like to perform some checks before allowing or denying the delete.
What I have done is to subscribe to the RowDeleting
event of the DataTable.
workingDataSet.Tables["realestates"].RowDeleting += Database_RowDeleting;
Then in the method that handles it, I just throw an exception. The actual logic will follow once I get this working.
void Database_RowDeleting(object sender, DataRowChangeEventArgs e)
{
throw (new Exception("Can't delete"));
}
This works and raises the exception when I am trying to delete any row from the DataGrid
. The problem though is that the DataGrid
control is not catching the exception (red border the DataGrid
control) which causes my program to shut down and is obviously not what I want. Any idea what I am doing wrong here? How can I get the DataGrid
to catch these exceptions?