Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Virtual mode in ListView?

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.

like image 200
Thorin Oakenshield Avatar asked Mar 24 '11 07:03

Thorin Oakenshield


2 Answers

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.

like image 68
26071986 Avatar answered Sep 19 '22 06:09

26071986


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?

like image 34
Iacopo Vettori Avatar answered Sep 20 '22 06:09

Iacopo Vettori