VB.NET(2010)
我正在尝试使用Value和Display项创建一个ComboBox。以下是相关的代码位。我最初尝试使用具有完全相同结果的数据表:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
cmbFromGroup.Items.Clear()
ItemList.Clear()
Item = New SelectionItem(KeyValue, DisplayValue)
'Link combobox and Item
ItemList.Add(Item)
cmbFromGroup.DataSource = ItemList
cmbFromGroup.DisplayMember = "Display"
cmbFromGroup.ValueMember = "Key"
Catch ex As Exception
Stop
End Try
End Sub
Private Sub cmbFromGroup_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbFromGroup.SelectedIndexChanged
Try
cmbFromMP3.Items.Clear()
Dim x As String = cmbFromGroup.SelectedValue
' <snip>
cmbFromMP3.SelectedIndex = 0
Catch ex As Exception
Stop
End Try
End Sub
Public Interface ISelectionItem
Property Key As String
Property Display As String
End Interface
Public Class SelectionItem
Implements ISelectionItem
Public Sub New(ByVal vKey As String, ByVal vDisplay As String)
_Key = vKey
_Display = vDisplay
End Sub
Public Property Key As String Implements ISelectionItem.Key
Public Property Display As String Implements ISelectionItem.Display
End Class
我遇到的问题是cmbFromGroup.SelectedValue不包含所选项目的值。
我得到的是(来自Watch) cmbFromGroup.SelectedValue {Player.SelectionItem} Object
我从cmbFromGroup.SelectedValue获得的唯一选项是Equals,GetHashCode,GetType,ReferenceEquals和ToString,其中没有一个能够像我期望的那样给我ValueMember。
如何从所选项目中获取ValueMember?
答案 0 :(得分:2)
在Combo上使用.text
:
Dim x As String = cmbFromGroup.text
答案 1 :(得分:1)
我最终提出的解决方案是
cmbFromGroup.SelectedIndex = -1
然后在cmbFromGroup_SelectedIndexChanged子中捕获它。
答案 2 :(得分:1)
我遇到了同样的问题并设法使用数据库(使用存储过程)。 以下是Code
的相关位'On Form load or Button Click (Depending on your application)
conString.Open() 'change conString to your connection string name
'Set Command for the stored procedure
Dim sqlComGetDirectorates As New SqlCommand
With sqlComGetDirectorates
.CommandType = CommandType.StoredProcedure
.Connection = conString
.CommandText = "ProcGetDirectorates" 'your stored procedure name
'if your stored procedure has parameters
'.Parameters.AddWithValue("@OfficerIdentity", txtPNum.Text)
End With
'Create Data Adapter (Using the stored procedure Command created earlier)
' Create a data table and Fill the Data adapter
Dim DatadptDirectorates As New SqlDataAdapter(sqlComGetDirectorates)
Dim dattabDirectorates As New DataTable
DatadptDirectorates.Fill(dattabDirectorates)
'Your Combobox
ComboDirectorate.DataSource = dattabDirectorates
ComboDirectorate.DisplayMember = "DirectorateName"
ComboDirectorate.ValueMember = "DirectorateCode"
'User will see DisplayMember in the Combobox
在Combobox的SelectedIndexChanged上'
txtDirectorate.Text = ComboDirectorate.SelectedItem(1)
''注意:Combobox.SelectedItem将在内部从''存储过程的结果中获取ROW ''您需要通过指定combobox.selecteditem(??)来获取所需的值 ''在你的结果中(来自存储过程)你有0 - xx列,为(??)选择合适的数字
希望这有助于。
答案 3 :(得分:0)
在尝试将值绑定到 ComboBox 项时,我遇到了类似的问题,我最终做的是:
Public Class Form1
Dim ValuesList As List(Of String) = New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 10
ComboBox1.Items.Add("Item" & i)
ValuesList.Add("Value" & i)
Next
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
Dim selectedItem = ComboBox1.SelectedItem
Dim selectedItemValue As String = ValuesList.Item(ComboBox1.SelectedIndex)
MsgBox(selectedItem & " " & selectedItemValue)
End Sub
End Class