There is probably a simple answer to this, but I cannot seem to find out where to catch exceptions thrown from an event, say from within a RowDeleting event of DataTable. I have done a small application with a DataView and when I delete a row I would like a confirmation dialog.
I have read at MSDN that one has the throw an exception in order to cancel the deletion of the row.
Fine, but where do I catch this exception? It is the main thread so I am clueless. This is the method implementing the event actions...
Code:
private void Table_RowDeleting(object sender, DataRowChangeEventArgs ea)
{
XmlDocument xml;
DialogResult result;
try
{
result = MessageBox.Show("Delete element for " + (string)ea.Row[1] + "?",
"Deleting...", MessageBoxButtons.OKCancel);
if(result == DialogResult.OK)
{
if(File.Exists(tbFilename.Text))
{
xml = new XmlDocument();
xml.Load(tbFilename.Text);
xml.DocumentElement.RemoveChild(
xml.DocumentElement.SelectSingleNode("item[@id='"
+ (int)ea.Row[0] + "']"));
xml.Save(tbFilename.Text);
}
else
throw new FileNotFoundException(
"The specified file was not found.");
}
else
throw new Exception("canceled");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
throw;
}
}
If the exception throwing solution is not the way to cancel row deletion in a DataTable, please let me know as well.
Thanks,
Jacob.