Reputation: 329
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?
Upvotes: 3
Views: 51558
Reputation: 1
tempString = cbxSettingsUnit(cbxSettingsUnit.SelectedIndex).Itemdata
Upvotes: 0
Reputation: 2146
Try this:
Dim row_v As DataRowView = comboBox1.SelectedItem
Dim row As DataRow = row_v.Row
Dim itemName As String = row(0).ToString()
Upvotes: 2
Reputation: 993
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)
Upvotes: -1
Reputation: 9981
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
Upvotes: 7
Reputation: 1
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
Upvotes: 0
Reputation: 28403
You have to use this
tempString = cbxSettingsUnit.Items(cbxSettingsUnit.SelectedIndex).ToString
Upvotes: 0