Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / Windows Forms

Virtual Mode ListView

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
11 Sep 2009CPOL2 min read 116K   6.5K   50   10
A listview running in virtual mode

Introduction

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).

Screenshot

Image 1

Background

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.

C#
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:

C#
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.

History

  • 11th September, 2009: First version

License

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


Written By
Software Developer (Senior) brightman objects software studios
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat is the difference? Pin
Ömer S. Doğru 20211-Dec-22 19:13
Ömer S. Doğru 20211-Dec-22 19:13 
QuestionListView in VirtualMode limited to 100,000,000 rows? Pin
Endy.Wu30-Jul-18 16:05
Endy.Wu30-Jul-18 16:05 
QuestionWhy not using database sqlite? Pin
k45d10-Jun-15 3:40
k45d10-Jun-15 3:40 
GeneralMy vote of 5 Pin
OBD-ense14-Jul-12 13:10
OBD-ense14-Jul-12 13:10 
Questionvirtual mode needed? Pin
sheniss1-Mar-10 18:37
sheniss1-Mar-10 18:37 
AnswerRe: virtual mode needed? Pin
yetibrain2-Mar-10 12:39
yetibrain2-Mar-10 12:39 
QuestionAdding items to a ListView in VitualMode ? Pin
Mohammad Dayyan16-Jan-10 0:01
Mohammad Dayyan16-Jan-10 0:01 
AnswerRe: Adding items to a ListView in VitualMode ? Pin
yetibrain17-Jan-10 11:08
yetibrain17-Jan-10 11:08 
Salam Alaikum _Mohammad_,

the exception that occurs at:

listView1.Items.Add(newItem);//Exception

is because you cannot add items to the listview when running in virtual mode.

Only when running in non-virtual mode, you can add items to the listview. It seems
that you don't need the virtual mode, so in this case, just set the virtual mode to
false and unsubscribe the RetrieveVirtualItem event, because it is not needed in
normal mode.

If you still want to use the virtual mode, you will have to provide your data when the
control requests for items, this happens only within the RetrieveVirtualItem-eventhandler
method. In this method, you must create a ListViewItem and ListViewSubItems as well whereby
the row-number passed into this method in the event argument object is the only key to your
data. However the control always requests only data for those lines that are visible.

You should place your query within listView1_RetrieveVirtualItem and create a single
ListViewItem based on the row number. If you have a different key to your data than the
row number, fill in a mapper object e. g. some collection that maps the row numbers
to the key of your record you have to query for. This is what i do in the example. Of course
this must be done before setting the virtual mode to true and setting a size greater than one
and of course NOT within the callback method.

Hope this helps,

yb

yetibrain

GeneralRe: Adding items to a ListView in VitualMode ? Pin
Mohammad Dayyan17-Jan-10 15:12
Mohammad Dayyan17-Jan-10 15:12 
GeneralRe: Adding items to a ListView in VitualMode ? Pin
yetibrain18-Jan-10 12:00
yetibrain18-Jan-10 12: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.