我有一个分配了数据源的组合框:
Public Sub DataBind(ByVal table As DataTable, ByVal DisplayMember As String, ByVal ValueMember As String)
cbox.DataSource = table
cbox.DisplayMember = DisplayMember
cbox.ValueMember = ValueMember
End Sub
我想迭代ComboboxItems中的每个项目并获得其ValueMember和DisplayMember。 我看了这篇文章ValueMember from ComboBox.Items[i] using WinForms c#
所以我试过
For I As Integer = 0 To cbox.Items.Count - 1
Dim val = DirectCast(cbox.Items(I), KeyValuePair(Of Integer, String)).Value
Next
但是我得到一个无效的强制转换异常,说明指定的强制转换是无效的。 我做错了什么?
答案 0 :(得分:1)
找到了我想要实现的目标的另一种解决方案:
Dim row As DataRow = DirectCast(cbox.Items(I), DataRowView).Row
Dim val As String = (row(cbox.ValueMember)).ToString()