我有一个组合框绑定到具有Status和StatusID的数据源“StatusListBindingSource”。我需要找到的项目的索引。我将使用索引来设置Me.cmbResult.SelectedIndex
除了我不想使用for / each循环之外,这个工作正常:
Private Sub SetStatusIndex(ByVal StatusValue As Integer)
Dim ItemIndex As Integer = -1
Dim ct As Integer = 0
For Each dView As DataRowView In Me.cmbResult.Items
If dView.Row.Item("StatusID") = StatusValue Then
ItemIndex = ct
Exit For
End If
ct += 1
Next
Me.cmbResult.SelectedIndex = ItemIndex
End Property
这是我想做的事情:
Dim a As ComboBox.ObjectCollection = Me.cmbResult.Items
'can't cast like this
Dim lisDRView As List(Of DataRowView) = a.Cast(Of DataRowView)()
'I'd like to use something like .find
Dim drView As DataRowView = lisDRView.Find(Function(x) x.Row.Item("StatusID") = StatusValue)
'there is no drView.index so this fails
Me.cmbResult.SelectedIndex = drView.index
但我在a.Cast(Of DataRowView)()
上收到例外,当然没有drView.index
InvalidCastException的: 无法投射物体 类型'd__aa'1 [System.Data.DataRowView]' 输入'System.Collections.Generic.List`1 [System.Data.DataRowView]'。
如何使用find?
获取给定StatusValue的项目索引答案 0 :(得分:0)
这有效:
Dim lis As List(Of DataRowView) = _
(From item As DataRowView In cmbResult.Items Select item).ToList
Dim d = lis.Find(Function(x As DataRowView) x.Row.Item("StatusID") = StatusValue)
Me.cmbResult.SelectedItem = d
的帮助下