Get Selected Item From Combobox

41,714

Solution 1

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)

Solution 2

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

Solution 3

Try this:

Dim row_v As DataRowView = comboBox1.SelectedItem
        Dim row As DataRow = row_v.Row
        Dim itemName As String = row(0).ToString()
Share:
41,714
PaulPolon
Author by

PaulPolon

Updated on July 09, 2022

Comments

  • PaulPolon
    PaulPolon almost 2 years

    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?