views:

41

answers:

2

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.

+2  A: 

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.

jcopenha
Could you please post a sample code? Thanks.
Mohammad
A: 

I've done it by the following code, But it has an Exception when selected items are more than one item !!!

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

List<ListViewItem> ListViewItems = new List<ListViewItem>();

foreach (int index in listView1.SelectedIndices)
{
    ListViewItem SelectedListViewItem = listView1.Items[index];//Exception
    ListViewItems.RemoveAt(index);
}
.
.
.
.
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    e.Item = ListViewItems[e.ItemIndex];
}
Mohammad