Displaying Computed Data in Virtual Mode

Another scenario where you may want to take explicit control over providing a display value for each cell is when you are working with data that contains computed values, especially in combination with bound data and large sets of data. For example, you may need to present a collection of data that has tens of thousands or even millions of rows in a grid. You really ought to guestion the use case first before supporting such a thing, but if you really need to do this, you probably don't want to clobber your system's memory by creating multiple copies of that data. You may not even want to bring it all into memory at one time, especially if this data is being dynamically created or computed. However, you will want users to able be to smoothly scroll through all that data to locate the items of interest.

Virtual mode for the DataGridView lets you display values for cells as they are rendered, so that data doesn't have to be held in memory when it's not being used. With virtual mode you can specify which columns the grid contains at design time, and then provide the cell values as needed, so they display at runtime but not before. The grid will internally only maintain the cell values for the cells that are displayed; you provide the cell value on an as-needed basis.

When you choose to use virtual mode, you can either provide all of the row values through virtual mode event handling, or you can mix the columns of the grid with data-bound and unbound columns. You have to define the columns that will be populated through virtual mode as described earlier in the "Programmatic DataGridView Construction" section. If you also data bind some columns, then the rows will be populated through data binding and you just handle the events necessary for virtual mode for the columns that aren't data bound. If you aren't data binding, you have to add as many rows as you expect to present in the grid, so the grid knows to scale the scrollbar appropriately. You then need a way to get the values corresponding to virtual mode columns when they are needed for presentation. You can do this by computing the values dynamically when they are needed, which is one of the main times that you would use virtual mode. You might also use cached client-side data in the form of an object collection or data set, or you might actually make round-trips to the server to get the data as needed. With the latter approach, you'll need a smart preload and caching strategy, because you could guickly bog down the application if data gueries have to run between the client and the server while the user is scrolling. Ifthe data being displayed is computed data, then it really makes sense to wait to compute values until they are actually going to be displayed.

Setting Up Virtual Mode

The following steps describe how to set up virtual mode data binding.

1. Create a grid and define the columns in it that will use virtual mode.

2. Put the grid into virtual mode by setting theVirtualMode property to TRue.

3. If you aren't using data binding, add as many rows to the grid as you want to support scrolling through. The easiest and fastest way to do this is to create one prototype row, then use the AddCopies method of the Rows collection to add as many copies of that prototype row as you would like. You don't have to worry about the cell contents at this point, because you are going to provide those dynamically through an event handler as the grid is rendered.

4. The final step is to wire up an event handler for the CellValueNeeded event on the grid. This event will only be fired when the grid is operating in virtual mode, and will be fired for each unbound column cell that is currently visible in the grid when it is first displayed and as the user scrolls.

The code in Listing 6.2 shows a simple Windows Forms application that demonstrates the use of virtual mode. /DataGridView object was added to the form through the designer and named m_Grid, and a button added to the form for checking how many rows had been

0 0

Post a comment