0

I have a combobox assigned with a data source:

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

I want to iterate each item in the ComboboxItems and get its ValueMember and DisplayMember. I watched this post ValueMember from ComboBox.Items[i] using WinForms c#

so I tried

For I As Integer = 0 To cbox.Items.Count - 1
  Dim val = DirectCast(cbox.Items(I), KeyValuePair(Of Integer, String)).Value
Next

but I get an invalid cast exception saying that the specified cast is not valid. What am I doing wrong?

itay312
  • 1,518
  • 4
  • 24
  • 36

1 Answers1

1

Found another solution to what I was trying to achieve:

Dim row As DataRow = DirectCast(cbox.Items(I), DataRowView).Row
Dim val As String = (row(cbox.ValueMember)).ToString()
itay312
  • 1,518
  • 4
  • 24
  • 36