8

i'm using VirtualMode to fill the columns like

List<ListViewItem> m_lstItem;


    private void Form1_Load(object sender, EventArgs e)
    {
        m_lstItem = Enumerable.Range(0, 100000).Select(X => new ListViewItem(new String[] { X.ToString(), (X + 1).ToString() })).ToList();
        listView1.VirtualListSize = m_lstItem.Count;
    }

    private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        e.Item = m_lstItem[e.ItemIndex];
    }

but i can not access the selected item. while accessing the selected item its throwing an error like Cannot access the selected items collection when the ListView is in virtual mode.

How do i get the selected items from the listView when it is in VirtualMode

Please help me to do this.

Sanjeevakumar Hiremath
  • 10,985
  • 3
  • 41
  • 46
Thorin Oakenshield
  • 14,232
  • 33
  • 106
  • 146

3 Answers3

10

From MSDN:

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.

26071986
  • 2,320
  • 3
  • 23
  • 34
2

The Items collection is not available as an iterable collection in Virtual Mode but it is always possible to access a single element using Items(SelectedIndices(0)). I found that it works also using FULLROWSELECT. The problem is referenced on another page of this same site: Cannot access the selected items collection when the ListView is in virtual mode?

Community
  • 1
  • 1
0

For some reason the SelectedIndices were always invalid when I tried to use them, maybe because of using FULLROWSELECT.

The selected item was available however even if the documentation wasn't clear. I found it with the ItemSelectionChanged event handler as e.ItemIndex.

Hope this might be helpful to someone else.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
ToniMarieM
  • 61
  • 4