To view a printable version of this article in a PDF viewer, click here for PDF.
Introduction
Back when Spread was originally created, computers were not as fast as they are today. A method was needed that allowed users to view large amounts of data. Back then no other component provided this buffering capability, and Spread was the first to provide a set of properties and events that would give the application developer better control over setting data and attributes for each cell in this "virtual mode". This article introduces the concept of virtual (buffering) mode and how it is used today in Spread. A future article will cover the more advanced topic of using virtual mode when the control is bound to a data source.
Running the Spread control in virtual mode can improve your control's performance considerably. Turning virtual mode on lets the control read into a buffer only the amount of data necessary to display the requested rows. You can adjust this buffer size to an optimal size to conserve system resources or to increase responsiveness, depending on your application's needs. With a few simple events and properties, you can make use of this feature.
Virtual Mode
One of the key elements of virtual mode is the QueryData event that allows the application to get the data into the spreadsheet. All correspondence between Spread and the application goes through the QueryData event. Within this event, the application not only supplies data and attributes to Spread, it also tells Spread when the top and bottom of the data is reached (Spread uses the same event internally when Spread is in bound virtual mode). The other event that is sent by Spread is the VirtualClearData event. It is sent before Spread discards the virtual buffer, allowing the application to clean up anything before the data is discarded.
One powerful aspect of virtual mode in Spread is virtual overlap. The virtual overlap, specified in the propery by that name is a way of buffering the data that has been previously viewed by the user. For instance, if the user pages down a couple of virtual pages, then decides to go back up and look at the data in the previous page, there is no reason to retrieve the data again. That data can be buffered by specifying an overlap buffer set to the number of desired buffered rows. An example of this may be setting the virtual page size to 50 using the VirtualRows property and setting the VirtualOverlap property to 100. This would allow two virtual pages to be buffered. Another option would be to set VirtualOverlap to a large number like 10,000. That way data would stay in memory as it is read, and discarded by Spread when no longer needed. Spread handles discarding rows that are no longer required to stay in memory.
When Spread is in virtual mode, it is strongly recommended not to display headers, or at least set the row labels to be blank, because row numbers have no meaning in virtual mode. The row number used in Spread does not correspond to the row in the data source, which can be very confusing to the end-user. Virtual mode was originally designed for data sources that did not know how many rows of data there were. As the user pages down, Spread begins placing the data in row one and proceeds down one row at a time. But if the user goes to the bottom, Spread places the data at an arbitrary row number, so that there is plenty of space above that row to scroll up. The application should not care where Spread chooses to place the data, because the row to begin placing the data is passed to the QueryData event. This information is also available at any time by using the VirtualCurTop property. The VirtualCurRowCount property can also be used by the application to determine how many rows are currently in the virtual buffer. Since Spread was not designed nor does it care exactly how many rows are in the data source, the VirtualMaxRows property is provided for the thumb scroll box of the virtual scroll bar. This allows the user to move through the rows using the thumb scroll box.
Virtual Overlap and Virtual Page Diagram
Special Scroll Bar for Virtual Mode
Since the thumb scroll box of the vertical scroll bar can be meaningless if the number of the rows is unknown, Spread has a new scroll bar that can be turned on using the VScrollSprecial property. This scroll bar does not have a thumb and displays buttons for line up/down, page up/down, home and end.
QueryData Event Details
As an application developer, you do not have to worry about when to supply data to Spread. Spread takes care of deciding when more data is needed using the QueryData event. The application responds by setting the requested rows with data or attributes or both. The arguments for the QueryData event are:
- Row
- RowsNeeded
- RowsLoaded
- Direction
- AtTop
- AtBottom
The first argument, Row is the first row to which data should be set. The RowsNeeded argument specifies how many rows of data should be set. Have the application set the RowsLoaded argument to the number of rows actually loaded into Spread. The other argument that is sent by Spread is the Direction. The direction may be Down, Up, Top, Bottom, or Refresh. Also, set the AtTop or AtBottom argument to True if the data is at the top or bottom of the data source.
Conclusion
The speed and performance gains from using Spread's unbound virtual mode makes it worth the effort of understanding these few properties and event arguments. The QueryData and VirtualClearData events are important, and the VirtualOverlap, VirtualRows, VirtualCurTop, VirtualCurRowCount, and VirtualMaxRows properties are helpful in customizing the use of virtual mode. There is a sample project on our Web site that gives a template for implementing unbound virtual mode that you can use in your application.
To view a printable version of this article in a PDF viewer, click here for PDF.
© 2004 FarPoint Technologies, Inc. All rights reserved. Spread, Spread for Web Forms, and Spread for Windows Forms are trademarks of FarPoint Technologies, Inc. Other brand and product names are trademarks or registered trademarks of their respective holders.