2016-07-29 57 views
0

我有一個組合框綁定到狀態和狀態ID的數據源「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

無效castexception: 無法投射類型爲'd__aa'1 [System.Data.DataRowView]' 的對象 來鍵入'System.Collections.Generic.List`1 [System.Data.DataRowView]'。

如何使用find獲得給定StatusValue的項目索引?

回答

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 

隨着從how to get the all combobox item .... using array

協助
相關問題