0

我正在动态创建一些控件并将它们绑定到数据库:

'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

我想要的是控件名称,如果这是不可能的,那么数据库中导致错误的列名。

4

1 回答 1

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)
于 2012-11-07T15:59:28.610 回答