Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access the selected items collection when the ListView is in virtual mode?

I have a ListView in Virtual Mode. I wanna to access SelectedItems property.
But when I use ListView1.SelectedItems , I receive the following Exception :

Cannot access the selected items collection when the ListView is in virtual mode

How can I access to ListView1.SelectedItems in VirtualMode.

like image 815
Mohammad Dayyan Avatar asked Oct 08 '10 20:10

Mohammad Dayyan


2 Answers

It is quite old post but maybe someone else will benefit.

Simply use ListView.SelectedIndexCollection col = listView.SelectedIndices; Then you can access an item:

forearch(var item in col)
{
   string txt = listView.Items[item].Text;
}

..but you won't be able to iterate through ListView.Items using foreach because there is no iterator available in this mode. Using indexer is just flying fine :-)

When trying to use foreach you get an exception:

When the ListView is in virtual mode, you cannot enumerate through the ListView items collection using an enumerator or call GetEnumerator. Use the ListView items indexer instead and access an item by index value.

like image 128
Mariusz Avatar answered Sep 19 '22 16:09

Mariusz


From the docs

In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.

like image 26
jcopenha Avatar answered Sep 20 '22 16:09

jcopenha