我在用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