我有一个包含两列c1&c2的数据表。
(C1具有allowdbnull=false)
. 数据表创建如下:
Private Function GetDataTable() As DataTable
Dim DT As New DataTable
'Create the first column
Dim C As New DataColumn("C1")
C.AllowDBNull = False
DT.Columns.Add(C)
'Second column
DT.Columns.Add(New DataColumn("C2"))
Return DT
End Function
然后我有一个表单,其中有两个文本框绑定到数据表:
Dim DT As DataTable = GetDataTable()
Dim CurrencyManager As CurrencyManager = CType(Me.BindingContext(DT), CurrencyManager)
'Add the bindings
TextBox1.BindingContext = Me.BindingContext
TextBox2.BindingContext = Me.BindingContext
TextBox1.DataBindings.Add(New Binding("text", DT, "C1", True, DataSourceUpdateMode.OnValidation))
TextBox2.DataBindings.Add(New Binding("text", DT, "C2", True, DataSourceUpdateMode.OnValidation))
'Set the null value of the Textbox1
TextBox1.DataBindings(0).NullValue = ""
我正在设置textbox1的空值,以便每当textbox为“”时,都应将其视为dbnull。
我使用currencymanager插入新行:
'Insert a new row
CurrencyManager.AddNew()
'Fill the two columns...
Dim Row As DataRowView = CurrencyManager.Current
Row.Row.Item(0) = "Column 1 Value"
Row.Row.Item(1) = "Column 2 Value"
'Validate the entry
CurrencyManager.EndCurrentEdit() 'No issue here since
现在,当用户清除firsttextbox(datatable列已允许全部为false)时,如果我运行以下代码两次。这个
首次引发异常时
但是,msgbox会显示出来
第二次没有引起异常
它收回前一个值,即“列1值”,并且该列不再是dbnull。
Try
CurrencyManager.EndCurrentEdit()
Catch ex As Exception
msgbox("The field C1 can not be empty")
End Try
我的问题是:当字段为empy时,是否有任何方法使最后一个代码总是引发异常?