寻找一种从ComboBox中填充的100多个值中获取所选字符串值的方法。 下面的函数旨在从DB获取预期值并将值返回给ComboBox。
Public Function popuList(Optional dt As DataTable = Nothing)
Dim daParties As New OleDb.OleDbDataAdapter("SELECT party_ID, party_Name FROM tbl_Parties ORDER BY party_Name;", connKMVTDB)
Dim dsParties As New DataSet
Dim dtParties As New DataTable
daParties.Fill(dsParties, "party_Name")
dtParties = dsParties.Tables(0)
Return dtParties
End Function
正如所料,以下事件也可以正常运作。
Private Sub btnPartyLOV_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnPartyLOV.Click
If cmbPartiesList.Visibility = Windows.Visibility.Visible Then
cmbPartiesList.Visibility = Windows.Visibility.Hidden
Else
Dim dtParties As DataTable
dtParties = popuList()
cmbPartiesList.ItemsSource = dtParties.DefaultView
cmbPartiesList.DisplayMemberPath = dtParties.Columns(1).ToString
cmbPartiesList.SelectedValuePath = dtParties.Columns(0).ToString
cmbPartiesList.Visibility = Windows.Visibility.Visible
'cmbPartiesList.SelectedIndex = 0
End If
End Sub
但是如何将ComboBox中的选定文本值转换为另一个TextBox?在下面试过并坚持......
Private Sub cmbPartiesList_SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbPartiesList.SelectionChanged
Dim selItem As String = DirectCast(cmbPartiesList.SelectedItem, ComboBoxItem).Content.ToString()
MsgBox(selItem)
lblID.Content = cmbPartiesList.SelectedValue
txtAdd1.Text = cmbPartiesList.DisplayMemberPath
cmbPartiesList.Visibility = Windows.Visibility.Hidden
End Sub
我反复收到错误......
无法将类型为“System.Data.DataRowView”的对象强制转换为类型 'System.Windows.Controls.ComboBoxItem'
这里搁置了很长时间,任何帮助都将受到高度赞赏和支持。好评。
答案 0 :(得分:0)
记住...... ToString()
是你的朋友!
这很简单:
Private Sub cmbPartiesList_SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbPartiesList.SelectionChanged
Dim selItem As String = cmbPartiesList.SelectedItem.ToString()
MsgBox(selItem)
lblID.Content = cmbPartiesList.SelectedValue
txtAdd1.Text = cmbPartiesList.DisplayMemberPath
cmbPartiesList.Visibility = Windows.Visibility.Hidden
End Sub
答案 1 :(得分:0)
我试过马修,毫无用处。看...
Private Sub cmbPartiesList_SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbPartiesList.SelectionChanged
Dim drv As DataRowView = cmbPartiesList.SelectedItem
txtPName.Text = drv.Item(1).ToString()
End Sub
这解决了我的问题,错误,怀疑等。 在这里,我创建了一个DataRowView类来存储在此调用期间传递的当前行项。结果被收集为一行并将它们与项目对象分开,这基本上暴露了基础结果。
无论如何,谢谢你的时间。 对上述内容的任何建议/更正/修改/改进/批评均受到高度赞赏。