Iteration of a selectedItemCollection
本文关键字:selectedItemCollection of Iteration | 更新日期: 2023-09-27 18:04:28
我有一个selectedItemCollection保存在一个对象中,该对象保存有关我从绑定数据网格中选择的选定行的信息。
我试图使用所选数据区内持有的信息,但我无法使其工作。
我已经尝试了下面的代码,但是没有运气,有没有一种简单的方法来迭代保存这些信息的对象?
一如既往地感谢您的帮助。
public void createRpt(string reportNum, Object selectedItems)
{
//Find the information held within selectedItems and do something with it.
foreach(var item in selectedItems as ItemCollection) //This returns a 'null exeption on itemCollection' error.
{
//Do something with items
}
}
转换为IList
应该为您工作:
public void createRpt(string reportNum, Object selectedItems)
{
var items = (System.Collections.IList)selectedItems;
//var typedItems = items.Cast<YourCustomType>();
foreach(var item in items)
{
//Do something with items
}
}
要检查为什么会出现这个错误以及它是从哪里来的,您可以将方法更改为下面并调试它:
public void createRpt(string reportNum, Object selectedItems)
{
//Add debugging code
var itemCollection = selectedItems as ItemCollection;
if(itemCollection == null)
{
Debug.WriteLine("selectedItems can not be casted to ItemCollection, or not initialized.");
return;
}
if(itemCollection.Count <= 0)
{
Debug.WriteLine("Collection is empty");
return;
}
//Find the information held within selectedItems and do something with it.
foreach(var item in itemCollection)
{
//Do something with items
}
}