PaulPolon PaulPolon - 1 year ago 117 Question

Get Selected Item From Combobox

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)
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
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?

Answer Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download