我正在动态创建一些控件并将它们绑定到数据库:
'childElem is an XElement containing data for the control,
'dpBindingSource is the bindingSource Connected with the database
Select Case cntrl.GetType
Case GetType(TextBox)
Dim txt As TextBox
txt = DirectCast(cntrl, TextBox)
txt.DataBindings.Add(New Binding("Text", dpBindingSource, childElem.Element("Col_Source").Value, True))
AddHandler txt.TextChanged, AddressOf controlValueChanged
Case GetType(ComboBox)
Dim cbo As ComboBox
cbo = DirectCast(cntrl, ComboBox)
With cbo
.DataSource = dt 'datatable to fill the combo
.DisplayMember = childElem.Element("Display_Member").Value
.ValueMember = childElem.Element("Value_Member").Value
.DataBindings.Add(New Binding("SelectedValue",dpBindingSource, childElem.Element("Col_Source").Value , True))
End with
AddHandler cbo.SelectedIndexChanged, AddressOf controlValueChanged
End Select
因此,当我尝试更新时,请检查dpBindingSource_BindingComplete。例如,如果文本框中的格式应该是数字,我会得到一个消息框:
Private Sub dpBindingSource_BindingComplete(sender As Object, e As System.Windows.Forms.BindingCompleteEventArgs) Handles dpBindingSource.BindingComplete
If Not e.BindingCompleteState = BindingCompleteState.Success Then
MessageBox.Show(e.ErrorText)
End If
End Sub
我想要的是控件名称,如果这不可能,那么导致错误的数据库中的列名称。
答案 0 :(得分:0)
我找到了解决方案:
MessageBox.Show("Error :" & e.ErrorText + vbCrLf & _
"Control : " & e.Binding.Control.Name.ToString + vbCrLf & _
"Field : " & e.Binding.BindingMemberInfo.BindingField.ToString + vbCrLf & _
"Info :" & e.Binding.BindableComponent.ToString + vbCrLf & _
"Correct Format :" & e.Exception.InnerException.TargetSite.DeclaringType.Name)