how to reject the invalid row adding to datatable?
-
dataTable1.RowChaging += new DataRowChangeEventHandler(dataTable1_rowChaing) private void dataTable1_RowChanging(object sender, DataRowChangeEventArgs e) { if ( e.Action == DataRowAction.Add ) { if (...) throw new ApplicationException("不合法的检查项目!"); } } ------------ the problem is : this dataTable is associated to a datagrid, when add a new row through the datagrid, the rowchanging event can be trigered. If the row is invalid, the exception will be throwed, and the new row cannot be added to the dataTable1. But when should I handle this Exception? I can't catch this exception.
-
dataTable1.RowChaging += new DataRowChangeEventHandler(dataTable1_rowChaing) private void dataTable1_RowChanging(object sender, DataRowChangeEventArgs e) { if ( e.Action == DataRowAction.Add ) { if (...) throw new ApplicationException("不合法的检查项目!"); } } ------------ the problem is : this dataTable is associated to a datagrid, when add a new row through the datagrid, the rowchanging event can be trigered. If the row is invalid, the exception will be throwed, and the new row cannot be added to the dataTable1. But when should I handle this Exception? I can't catch this exception.
You could create a handler for
AppDomain.UnhandledException
, but it might be simpler to just display an error or warning message box to the user withMessageBox.Show
. With Windows XP and higher, they are now using balloons when invalid characters are typed, such as a backslash in a filename or another invalid character somewhere. You could always do something like that, too. Basically, just don't throw the exception and instead provide feedback to the user.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
You could create a handler for
AppDomain.UnhandledException
, but it might be simpler to just display an error or warning message box to the user withMessageBox.Show
. With Windows XP and higher, they are now using balloons when invalid characters are typed, such as a backslash in a filename or another invalid character somewhere. You could always do something like that, too. Basically, just don't throw the exception and instead provide feedback to the user.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
But if I don't throw the exception in RowChaging event, i can't prevent the new row to be added to the data table. Or some way to do that?
Sorry about that. I've been doing so much control development lately I'm too used to such events being
CancelEventHandler
events (or similar)! Yes, you'll have to throw the exception so add a handler toAppDomain.UnhandledException
like I mentioned before. I recommend you also create a custom exception class that derives fromApplicationException
(typically used for custom exceptions to give them a common, application-based base class) that you throw from theDataTable.RowChanging
event handler. This way, you can easily just catch this exception in the handler for theAppDomain.UnhandledException
event and handle appropriately, which is better than throwing such a oft-used exception and trying to determine where it came from and why it was thrown.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Sorry about that. I've been doing so much control development lately I'm too used to such events being
CancelEventHandler
events (or similar)! Yes, you'll have to throw the exception so add a handler toAppDomain.UnhandledException
like I mentioned before. I recommend you also create a custom exception class that derives fromApplicationException
(typically used for custom exceptions to give them a common, application-based base class) that you throw from theDataTable.RowChanging
event handler. This way, you can easily just catch this exception in the handler for theAppDomain.UnhandledException
event and handle appropriately, which is better than throwing such a oft-used exception and trying to determine where it came from and why it was thrown.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
I did that. And I throw a ErrorDataRowException which inherits from Application in the data table's RowChanging event. But when the ErrorDataRowException is thrown, the application didnot enter the method UnHandledExceptionHandler. Instead, the application still popup an window in which told me there is an unhandled exception. why? pls help me. [STAThread] static void Main() { AppDomain currentAppDomain = AppDomain.CurrentDomain; currentAppDomain.UnhandledException += new UnhandledExceptionEventHandler(UnHandledExceptionHandler); FormRegisterConsole fmMain = new FormRegisterConsole(); Application.Run(fmMain); } private static void UnHandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; if (e.GetType() == typeof(Health.ApplicationLibrary.RIS.Register.ErrorDataRowException)) { MessageBox.Show(e.Message, "data updating", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("出现未处理错误: " + e.Message, "未处理错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
-
I did that. And I throw a ErrorDataRowException which inherits from Application in the data table's RowChanging event. But when the ErrorDataRowException is thrown, the application didnot enter the method UnHandledExceptionHandler. Instead, the application still popup an window in which told me there is an unhandled exception. why? pls help me. [STAThread] static void Main() { AppDomain currentAppDomain = AppDomain.CurrentDomain; currentAppDomain.UnhandledException += new UnhandledExceptionEventHandler(UnHandledExceptionHandler); FormRegisterConsole fmMain = new FormRegisterConsole(); Application.Run(fmMain); } private static void UnHandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; if (e.GetType() == typeof(Health.ApplicationLibrary.RIS.Register.ErrorDataRowException)) { MessageBox.Show(e.Message, "data updating", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("出现未处理错误: " + e.Message, "未处理错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
Try
Application.ThreadException
then. I use the same thing in our program I designed at work but couldn't remember which event I was using. It's been a long time since I wrote that section of the container application. Also, just a helpful tip: try theis
keyword instead ofobj.GetType() == typeof(SomeClass)
. It results in fewer IL instructions (better performance) and, IMO, is a little easier to read:public static void Main(string[] args)
{
Application.ThreadException +=
new ThreadExceptionEventHandler(UnhandledException);
Application.Run(new FormRegisterConsole());
}
private static void UnhandledException(object sender,
ThreadExceptionEventArgs e)
{
if (e.Exception is ErrorDataRowException)
{
//...
}
else
{
//...
}
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Try
Application.ThreadException
then. I use the same thing in our program I designed at work but couldn't remember which event I was using. It's been a long time since I wrote that section of the container application. Also, just a helpful tip: try theis
keyword instead ofobj.GetType() == typeof(SomeClass)
. It results in fewer IL instructions (better performance) and, IMO, is a little easier to read:public static void Main(string[] args)
{
Application.ThreadException +=
new ThreadExceptionEventHandler(UnhandledException);
Application.Run(new FormRegisterConsole());
}
private static void UnhandledException(object sender,
ThreadExceptionEventArgs e)
{
if (e.Exception is ErrorDataRowException)
{
//...
}
else
{
//...
}
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----