我在用WinForm ListView控件使用foreach語句時遇到了困難。以下兩個代碼塊演示了我正在嘗試做的事情。它與for循環一起工作,但不是foreach。通過列表查看和訪問子項目?
foreach(var item in listView.Items){
item. <-Can't access any of the subitems of this item
}
VS
for(int i=0;i<listView.Items.Count;i++){
listView.Items[i].Subitems[1] <- Here I can access the sub items
}
我嘗試使用foreach循環,所以我可以更容易地刪除ListView的項目。
爲什麼在這種情況下var不能滿足要求? – sooprise 2011-04-05 14:18:25
什麼其他情況是不夠的? – sooprise 2011-04-05 14:18:49
Items集合類型是Object。這就是爲什麼推理將var後面的變量作爲Object來執行的原因。如果您手動添加它們,ListView Items可以是ListViewItem的集合,也可以是使用DataSource的自己對象的集合。 – Larry 2011-04-05 14:30:41