3
votes

I have a combobox with items from a DataTable, the ff executes when the form loads:

dbConnection = New SqlCeConnection("Data Source=Journal.sdf")
dbDataAdapter = New SqlCeDataAdapter("SELECT * FROM setting_unit", dbConnection)
dbDataAdapter.Fill(dbTable)
cbxSettingsUnit.DataSource = New BindingSource(dbTable, Nothing)
cbxSettingsUnit.DisplayMember = "name"
cbxSettingsUnit.ValueMember = "name"

The method when there is a changed in the combox box:

Private Sub cbxSettingsUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxSettingsUnit.SelectedIndexChanged
   tempString = cbxSettingsBusinessUnit.Items(cbxSettingsBusinessUnit.SelectedItem).ToString
   MessageBox.Show(tempString)
End Sub

there is an error at the line:

tempString = cbxSettingsBusinessUnit.Items(cbxSettingsBusinessUnit.SelectedItem).ToString

How do I get the selected item from the combox box?

5

5 Answers

7
votes

Most of the .net "listing-controls" who have a DataSource property search for the implementation of the IListSource. So if you set a DataTable as datasource, you're actually setting the DataTable.DefaultView as the datasource.

Me.ComboBox1.DataSource = myDataTabele

Equals

Me.ComboBox1.DataSource = myDataTabele.DefaultView

So now you have a ComboBox packed with items of type DataRowView.

Dim selectedRowView As DataRowView = DirectCast(Me.ComboBox1.SelectedItem, DataRowView)
Dim selectedDisplayMemberValue As String = Me.ComboBox1.SelectedText
Dim selectValueMemberValue As Object = Me.ComboBox1.SelectedValue

And this is how you should do it:

Private Sub cbxSettingsUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxSettingsUnit.SelectedIndexChanged
    Dim item As DataRowView = TryCast(Me.cbxSettingsUnit.SelectedItem, DataRowView)
    If (Not item Is Nothing) Then
        With item.Row
            'You have now access to the row of your table.
            Dim columnValue1 As String = .Item("MyColumnName1").ToString()
            Dim columnValue2 As String = .Item("MyColumnName2").ToString()
        End With
    End If
End Sub

So why did you get an error? Yes, you are trying to cast a DataRowView to an Integer.

'                                                                      |
tempString = cbxSettingsBusinessUnit.Items(cbxSettingsBusinessUnit.SelectedItem).ToString
2
votes

Try this:

Dim row_v As DataRowView = comboBox1.SelectedItem
        Dim row As DataRow = row_v.Row
        Dim itemName As String = row(0).ToString()
0
votes

You have to use this

tempString = cbxSettingsUnit.Items(cbxSettingsUnit.SelectedIndex).ToString
0
votes

try using below code

Try
        DS = New DataSet
        Tabel = "SELECT * FROM setting_unit"
        Dim CB As New OleDb.OleDbDataAdapter(Tabel, dbConnection)
        CB.Fill(DS, "setting_unit")
    '
        Me.cbxSettingsUnit.DataSource = Prf.Tables(0)

        Me.cbxSettingsUnit.ValueMember = "name"
        Me.cbxSettingsUnit.DisplayMember = "name"
    Catch ex As Exception
    End Try
-1
votes

try this:

combo.datasource = datatable
combo.displaymember = "abbreviation"
combo.valuemember = "abbreviation"

then getting the selected item use this:

on SelectionChangeCommitted event of combo box put this:

    tmpString=combo.selectedvalue
    msgBox(tmpString)