Exception from RowChanging. How to catch?

Hello all!
I have special class called ConnectedDataTable.
It's a child of DataTable.
In RowChanging and RowDeleting events it throws an exception. But my GridDataBoundGrid doesn't catch it. How can I catch such exception?

4 Replies

AD Administrator Syncfusion Team March 19, 2007 04:51 PM UTC

Hi c4p,

Please try to provide us some more information on this issue. I tried to reproduce the issue, but couldn't. kindly provide us a small sample to reproduce the issue or modify the browser sample accordingly. This will help us to analyse the issue further.

Best Regards,
Haneef


C4 c4p March 20, 2007 07:07 AM UTC

Hello!
Thanks for the reply.

I've reconstructed my problem in short code fragment:
1. Create Windows Froms Application.
2. Add GridDataBoundGrid to the form.
3. Add this code:

private DataTable myTable;
private void Form1_Load(object sender, System.EventArgs e)
{
myTable = new DataTable("Hello");
myTable.Columns.Add("SomeCol", typeof(String));

myTable.RowDeleting += new DataRowChangeEventHandler(myTable_RowDeleting);

gridDataBoundGrid1.DataSource = myTable;
}

private void myTable_RowDeleting(object sender, DataRowChangeEventArgs e)
{
throw (new Exception("Some text."));
}

Then, after adding rows I trying to delete row and get unhandled exception. How can I catch this exception?

My Platform: .Net 1.1, Syncfusion 4.1.0.62.


AD Administrator Syncfusion Team March 20, 2007 10:35 PM UTC

Hi C4p,

The reason for getting this exception is that you are throwing the exception (throw (new Exception("Some text."));) in RowDeleting event. If you want to handle the exception then use the Try...Catch block statement in a RowDeleting event. Here is a code snippet.

private void myTable_RowDeleting(object sender, DataRowChangeEventArgs e)
{
try
{
throw (new Exception("Some text."));
}
Catch(Exception ex)
{
Console.WriteLine("User thrown the Exception in RowDeleting event");
}
}


Best regards,
Haneef


C4 c4p March 21, 2007 10:54 AM UTC

Hello Haneef.
It not that I really need. I need that control catch the exception and warn the user about it. I can't catch inside RowDeleting event.

I found the solution for deleting rows:

private void grid_RowsDeleting(object sender, GridRowRangeEventArgs e)
{
GridRangeInfoList rangeList = this.grid.Selections.GetSelectedRows(true, false);

if(rangeList.Count <= 0)
return;

ArrayList a = new ArrayList();
CurrencyManager cm = (CurrencyManager)this.grid.BindingContext[this.grid.DataSource, this.grid.DataMember];

foreach(GridRangeInfo range in rangeList)
{
for(int row = range.Top; row <= range.Bottom; ++row)
{
int pos = this.grid.Binder.RowIndexToPosition(row);
a.Add(-pos);
}
}

a.Sort();
foreach(int pos in a)
{
try
{
cm.List.RemoveAt(-pos);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
e.Cancel = true;
}

But how can I create and edit rows in DataTable from RowEditing event in the same manner?

Loader.
Up arrow icon