2017-05-03 31 views
-1

我想知道如何訪問ListViewDataList的成員ListViewDataItem的數據鍵。如何訪問ListViewDataItem datakeys

我感到奇怪的是,在調試時,可以訪問這裏可以看出

enter image description here

但是當試圖編碼它是不可能期間訪問DataKeyContainer,它也可以是DataKeysContainer這裏

enter image description here

任何意見上看到如何訪問datakeys和值的ListViewDataItem將不勝感激。

+1

在第二個屏幕截圖中,您已經投射到「ListViewItem」 –

+0

@AFriend是正確的。將其更改爲「For Each l as ListViewDataItem」,它應該按照您的要求工作。 – Jaxi

+0

道歉,愚蠢的錯誤,但是,改變爲ListViewDataItem時,問題仍然存在,我仍然無法訪問DataKeys - 查看新的和更新的第二屏幕截圖。 – AF1001

回答

0

您可以使用後期綁定,即將您的循環變量放在Object中。這允許您延遲成員驗證,直到運行時訪問成員。它與c#中的動態類似。

For Each itemObject As Object In lstViewModules.Items 
    ' the DataKeysContainer you were looking for 
    Dim container = CType(itemObject.DataKeysContainer, Control) 
    ' the same l in your loop 
    Dim l = CType(itemObject, ListViewDataItem) 
    ' from here on, the rest of your loop code should work 
    Dim key As DataKey = lstViewModules.DataKeys(l.DataItemIndex) 
    Dim value = key("name") 
Next 

既然你知道這是一個ListView,你也可以施放containerListView設計時輕鬆自如。

注意:您必須有Option Strict Off才能正常工作。

+1

非常感謝你,我挽救了我從我的頭髮中拔出最後一根頭髮 – AF1001

+0

只是一個免責聲明,這是繞過了設計師的原意,只能在絕對必要時才使用:) – djv

+0

哦,我看到了,以及它的一些非常有用的知識,在幫助我度過美好時光之後。再次感謝您的幫助。 – AF1001