简体   繁体   中英

Catch exception for null value in a DataTable

I have an application in C# (winforms) that uses code generated by wizards for database management (datasets, datatables, etc.). If for example I have a datacolumn configured to allow null values (property AllowDBNull = true) and when a null value is entered (in a form) is configured to throw an exception (property NullValue = ThrowException). When in a form a textfield is edited and left blank it won't let the user switch focus to another control, I think it might be because of the NullValue property to throw an exception).The question is: how can I catch the exception and set a proper value.

我不确定您是否可以使用它来捕获异常,但是您可以为FocusLost等事件绑定一个事件,以检查字段的值并为其提供适当的值,如上一条语句的后半部分所述..这也可能会使捕获异常的需求过时了。

If your controls were bound to the DataSet in the designer, try setting the Null Value property in the Advanced Bindings section of the control's properties to your desired value. I imagine there is a similar way to do this in code if you set up your bindings that way.

Cheers! JE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM