【发布时间】:2019-12-01 12:31:51
【问题描述】:
我收到一个 DataBinding 错误,其中我的 ComboBox 绑定到 KeyValuePair 列表并且存储到 DataBase 的值是一个整数。
如何转换它?
到目前为止,这是我的代码:
Private Sub SetupeCombo()
Dim comboSource = New List(Of KeyValuePair(Of Integer, String))
For Each o In _List
comboSource.Add(New KeyValuePair(Of Integer,String)(o.ID, o.Details))
Next
comboBox1.DisplayMember = NameOf(Table.Details)
comboBox1.ValueMember = NameOf(Table.ID)
comboBox1.DataSource = New BindingSource(comboSource, Nothing)
End Sub
Public Sub BindComboBox()
Dim comboBoxBindings = comboBox1.DataBindings.Add(NameOf(ComboBox.SelectedValue), dataSource, NameOf(dataSource.ID), True, DataSourceUpdateMode.OnPropertyChanged, String.Empty)
AddHandler comboBoxBindings.BindingComplete, AddressOf comboBoxBindings _BindingComplete
End Sub
Private Sub comboBoxBindings_BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)
If e.BindingCompleteState <> BindingCompleteState.Success Then MessageBox.Show("Test: " & e.ErrorText)
End Sub
错误
测试:Value[1, TestValue] 无法转换为类型“ID”
【问题讨论】:
-
为什么不使用
_List或Table中的内容(不清楚数据的来源是什么)作为BindingSource 的DataSource?因此,您可以在设置DisplayMember和绑定时直接使用字段名称。看起来你让它变得比它需要的更复杂。
标签: vb.net winforms data-binding keyvaluepair