vb.net 从组合框中获取所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20780229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Selected Item From Combobox
提问by PaulPolon
I have a combobox with items from a DataTable, the ff executes when the form loads:
我有一个包含 DataTable 项目的组合框,ff 在表单加载时执行:
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?
如何从组合框中获取所选项目?
采纳答案by Unknownymous
try this:
尝试这个:
combo.datasource = datatable
combo.displaymember = "abbreviation"
combo.valuemember = "abbreviation"
then getting the selected item use this:
然后获取所选项目使用此:
on SelectionChangeCommittedevent of combo box put this:
在组合框的SelectionChangeCommitted事件上放这个:
tmpString=combo.selectedvalue
msgBox(tmpString)
回答by Bj?rn-Roger Kringsj?
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.
大多数具有属性搜索的 .net “列表控件”都DataSource
对IListSource的实现进行了搜索。因此,如果您将 a 设置DataTable
为数据源,则实际上是将 设置DataTable.DefaultView
为数据源。
Me.ComboBox1.DataSource = myDataTabele
Equals
等于
Me.ComboBox1.DataSource = myDataTabele.DefaultView
So now you have a ComboBox packed with items of type DataRowView
.
所以现在你有一个 ComboBox 装满了类型的项目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.
那么为什么会出现错误呢?是的,您正在尝试将 DataRowView 转换为整数。
' |
tempString = cbxSettingsBusinessUnit.Items(cbxSettingsBusinessUnit.SelectedItem).ToString
回答by Leebeedev
Try this:
尝试这个:
Dim row_v As DataRowView = comboBox1.SelectedItem
Dim row As DataRow = row_v.Row
Dim itemName As String = row(0).ToString()
回答by Vignesh Kumar A
You have to use this
你必须使用这个
tempString = cbxSettingsUnit.Items(cbxSettingsUnit.SelectedIndex).ToString
回答by user2999319
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