Loop all items in a ListView in virtual mode

I have a reporting module that creates PDF reports from ListViews.

Now I have a ListView in virtual mode, and therefore I cannot loop over the Items collection.

How do I iterate over all the elements in a list view from a reporting module?

I can get the VirtualListSize property, so I know how many elements are in the list. Can I somehow call RetreiveVirtualItem explicitly?

The Reporting module does not know about the list of supports in the ListView.

0
source share
4 answers

In a virtual ListView, you cannot iterate over Items , but you can access them by index:

 for (int i = 0; i < theVirtualListView.VirtualListSize; i++) { this.DoSomething(theVirtualListView.Items[i]); } 
+2
source

So, a list in virtual mode is just a visualization of your main list, right?

Perhaps the report should receive data from the base list instead of presenting a virtual list.

+4
source

The best solution I came across is to have a delegate in the report class, where the same delegate is passed as in ListView.RetrieveVirtualItem.

 class Report { [...] // Called when the content of an VirtualItem is needed. public event RetrieveVirtualItemEventHandler RetrieveVirtualItem; [...] private AddRows() { for (int i = 0; i < GetItemCount(); i++) AddRow(GetItem(i)); } private ListViewItem GetItem(n) { if (_listView.VirtualMode) return GetVirtualItem(n); return _listView.Items[n]; } private ListViewItem GetVirtualItem(int n) { if (RetrieveVirtualItem == null) throw new InvalidOperationException( "Delegate RetrieveVirtualItem not set when using ListView in virtual mode"); RetrieveVirtualItemEventArgs e = new RetrieveVirtualItemEventArgs(n); RetrieveVirtualItem(_listView, e); if (e.Item != null) { return e.Item; } throw new ArgumentOutOfRangeException("n", "Not in list"); } private static int GetItemsCount() { if (_listView.VirtualMode) return _listView.VirtualListSize; return _listView.Items.Count; } } 
+1
source

You can always list the main list in the outside world:

 foreach (object o in virtListView.UnderlyingList) { reportModule.DoYourThing(o); } 
+1
source

All Articles