Click here to Skip to main content
12,758,605 members (33,679 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 10 Sep 2009

Virtual Mode ListView

, 11 Sep 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A listview running in virtual mode


A WindowsForms ListView control can be used in "virtual mode". When running in virtual mode, the ListView doesn't host any data, instead the only thing it needs to know is, how many rows/lines it has. A callback method has to be provided in order to pass over the data to display, whenever the visible lines the control is able to display at a given time, scroll into view. The example is able to display about 100 million lines and represents the line number as text (German).



This simple prototype was created before I started creating the TreeListView as an extension to the ListView - also running in virtual mode - which is also available here at CodeProject: VirtualModeTreeListView.

Using the Code

In order for a ListView to operate in virtual mode, we will have to set the appropriate property "VirtualMode" to true. Once we place the WindowsForms ListView control on some form, the properties window of the ListView will appear. Be sure that the ListView object is selected.

activating the virtual mode

After activating the virtual mode, it's a must to subscribe to the "RetrieveVirtualItem" event as well, due to the fact that the control gathers for data regarding the rows that are visible/in view. If we forget to subscribe to this event, the control will fire an exception. So we kindly fulfill the requirements.

provide the retrieve virtual item event

Now we will have to provide some code within the callback handler of the event "RetrieveVirtualItem". Whenever lines are visible, the control gathers for data in order to display data within the columns of the control. Therefore the control expects a usual ListViewItem object, composed with any additional ListViewSubItem objects as child objects, depending on the configured columns of the control. Keep in mind that if we were not using the virtual mode, we'd have to fill the control with such objects so that the control could view data in its columns of each row. If not, the control would just be "empty".

This example just takes the line numbers that are passed into the RetrieveVirtualItem callback method and MakeText() creates the textual representation of the line numbers.

private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
       ListViewItem lvi = new ListViewItem(); 	// create a listviewitem object
       lvi.Text = nt.MakeText(e.ItemIndex); 		// assign the text to the item
       ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem(); // subitem
       NumberFormatInfo nfi = new CultureInfo("de-DE").NumberFormat;
       nfi.NumberDecimalDigits = 0;
       lvsi.Text = e.ItemIndex.ToString("n", nfi); 	// the subitem text
       lvi.SubItems.Add(lvsi); 			// assign subitem to item

       e.Item = lvi; 		// assign item to event argument's item-property

The MakeText() method just creates the appropriate string for a certain number, the text however is in German. There is a class called NumberText which is used to create the string. Please refer to the source file NumberText.cs for details.

One thing we still have to do is tell the ListView how many rows/lines it has. I do this within the Load event of the Form like this:

private void Form1_Load(object sender, EventArgs e)
            listView1.VirtualMode = true; 		// switching virtual mode on
            listView1.VirtualListSize = 1000000000; 	// give it 1 million lines

Points of Interest

You might also be interested in a treelistview running in virtual mode: VirtualModeTreeListView.


  • 11th September, 2009: First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) brightman objects software studios
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhy not using database sqlite? Pin
Member 1145609510-Jun-15 4:40
memberMember 1145609510-Jun-15 4:40 
GeneralMy vote of 5 Pin
OBD-ense14-Jul-12 14:10
memberOBD-ense14-Jul-12 14:10 
Questionvirtual mode needed? Pin
sheniss1-Mar-10 19:37
membersheniss1-Mar-10 19:37 
AnswerRe: virtual mode needed? Pin
yetibrain2-Mar-10 13:39
memberyetibrain2-Mar-10 13:39 
QuestionAdding items to a ListView in VitualMode ? Pin
_Mohammad_16-Jan-10 1:01
member_Mohammad_16-Jan-10 1:01 
Hi yetibrain and thanks for the article.
I have a question,
I'm gonna add an item to a ListView in VirtualMode, I have done whatever you said,
But when I add the Item to the ListView, the following Exception has occurred :
When the ListView is in virtual mode, you cannot add items to the ListView items collection. Use  the VirtualListSize property instead to change the size of the ListView items collection.

Below is the code that I have been using it :
listView1.VirtualMode = true;
listView1.VirtualListSize = query.Count();
foreach (var item in query)
    string tableNumber = item.TableNumber.HasValue ? item.TableNumber.Value.ToString() : "";
    ListViewItem newItem = new ListViewItem(new string[] { i.ToString(), 
                                                           DateTime.Now.ToString() });
    newItem.Font = new Font("Tahoma", 12);
    newItem.Name = "Item" + i.ToString();


void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    e.Item = new ListViewItem(e.ItemIndex.ToString());

Could you please guide me ?
AnswerRe: Adding items to a ListView in VitualMode ? Pin
yetibrain17-Jan-10 12:08
memberyetibrain17-Jan-10 12:08 
GeneralRe: Adding items to a ListView in VitualMode ? Pin
_Mohammad_17-Jan-10 16:12
member_Mohammad_17-Jan-10 16:12 
GeneralRe: Adding items to a ListView in VitualMode ? Pin
yetibrain18-Jan-10 13:00
memberyetibrain18-Jan-10 13:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 11 Sep 2009
Article Copyright 2009 by yetibrain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid