我有一个listview,需要遍历所选的项目集合。但我收到的错误是我的代码:
DataRowView drv;
foreach(var current in lstValue.SelectedItems)
{
ListBoxItem li = new ListBoxItem();
drv = current as DataRowView;
li.Content = Criteria + "=" + drv["CODE"];
lstSelection.Items.Add(li);
}
我将错误对象引用设置为未设置为对象的实例。这是我已查找的堆栈溢出链接
答案 0 :(得分:0)
你应该检查:
if( lstValue != null)
{
foreach(var current in lstValue.SelectedItems)
{
// Your code
}
}
答案 1 :(得分:0)
DataRowView drv;
foreach(var current in lstValue.SelectedItems)
{
ListBoxItem li = new ListBoxItem();
drv = current as DataRowView;
if (drv != null)
{
li.Content = Criteria + "=" + drv["CODE"];
lstSelection.Items.Add(li);
}
}