tags:

views:

183

answers:

1

Exact duplicate of How would you only draw certain ListView Items while in Virtual Mode? from same name, different account.

@Jonathan: please enhance your question instead of entering a new copy.


I am trying to implement a filter mechanism into a listview object (in virtual mode). I have gotten advice to not return the items in question in the retrieve_item event that I do not want displayed, but when I do not return anything less than a listview item (cached from an array of listviewitems holding all my listview items in question) I get an exception error saying that I must return a valid ListViewItem in the RetrieveVirtualItem event like it reads in the msdn it will do.

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.retrievevirtualitem.aspx

So how can I go upon only deciding like items [0], [5], and [11] to be displayed out of a list of let's say listviewitems[25] when I call out one of my own methods so that it does so?

After my task is done with what ever I want the filter to be used for, I want to revert all the original items back into the listview, how could I come to implement a feature like this?

  // Initialized with 25 listviewitem & subitems objects later during the programs runtime.
  public ListViewItem[] lviCache;

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

    void UnfilterlvItems()
    { 
        // How would I revert it back so that it draws all original items
        // from my listviewitem[] array back to normal to display
        // all 25 objects again?
    }

    void FilterlvItems()
    { 
        // What would I be putting in here so that I can fire off the
        // retrievevirtualitem events and only decide which items I want
        // display for the time being? {0, 5, 11 }
    }
A: 

You need to do this:

  • Build an array, filteredItems, of the indexes of the items you want to show. This will look like [0, 5, 11] in your example
  • Tell the control to show 3 items: resultsList.VirtualListSize = filteredItems.Count;
  • In RetrieveVirtualItem, return the item from your array:

    return lviCache[filteredItems[ev.ItemIndex]];

RichieHindle