I have a listview and will need to loop through the selected item collection. But I am getting an error below is my code:
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);
}
I get the error object reference not set to the instance of an object. This is the stack over flow link I already looked up
C# WPF - Get the selected items from a ListView
You should check:
if( lstValue != null)
{
foreach(var current in lstValue.SelectedItems)
{
// Your code
}
}
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);
}
}