Compartir a través de


Virtual Mode in the Windows Forms DataGridView Control

With virtual mode, you can manage the interaction between the DataGridView control and a custom data cache. To implement virtual mode, set the VirtualMode property to true and handle one or more of the events described in this topic. You will typically handle at least the CellValueNeeded event, which enables the control look up values in the data cache.

Bound Mode and Virtual Mode

Virtual mode is necessary only when you need to supplement or replace bound mode. In bound mode, you set the DataSource property and the control automatically loads the data from the specified source and submits user changes back to it. You can control which of the bound columns are displayed, and the data source itself typically handles operations such as sorting.

Supplementing Bound Mode

You can supplement bound mode by displaying unbound columns along with the bound columns. This is sometimes called "mixed mode" and is useful for displaying things like calculated values or user-interface (UI) controls.

Because unbound columns are outside the data source, they are ignored by the data source's sorting operations. Therefore, when you enable sorting in mixed mode, you must manage the unbound data in a local cache and implement virtual mode to let the DataGridView control interact with it.

For more information about using virtual mode to maintain the values in unbound columns, see the examples in the System.Windows.Forms.DataGridViewCheckBoxColumn.ThreeState property and System.Windows.Forms.DataGridViewComboBoxColumn class reference topics.

Replacing Bound Mode

If bound mode does not meet your performance needs, you can manage all your data in a custom cache through virtual-mode event handlers. For example, you can use virtual mode to implement a just-in-time data loading mechanism that retrieves only as much data from a networked database as is necessary for optimal performance. This scenario is particularly useful when working with large amounts of data over a slow network connection or with client machines that have a limited amount of RAM or storage space.

For more information about using virtual mode in a just-in-time scenario, see Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control.

Virtual-Mode Events

If your data is read-only, the CellValueNeeded event may be the only event you will need to handle. Additional virtual-mode events let you enable specific functionality like user edits, row addition and deletion, and row-level transactions.

Some standard DataGridView events (such as events that occur when users add or delete rows, or when cell values are edited, parsed, validated, or formatted) are useful in virtual mode, as well. You can also handle events that let you maintain values not typically stored in a bound data source, such as cell ToolTip text, cell and row error text, cell and row shortcut menu data, and row height data.

For more information about implementing virtual mode to manage read/write data with a row-level commit scope, see Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.

For an example that implements virtual mode with a cell-level commit scope, see the VirtualMode property reference topic.

The following events occur only when the VirtualMode property is set to true.

Event Description

CellValueNeeded

Used by the control to retrieve a cell value from the data cache for display. This event occurs only for cells in unbound columns.

CellValuePushed

Used by the control to commit user input for a cell to the data cache. This event occurs only for cells in unbound columns.

Call the UpdateCellValue method when changing a cached value outside of a CellValuePushed event handler to ensure that the current value is displayed in the control and to apply any automatic sizing modes currently in effect.

NewRowNeeded

Used by the control to indicate the need for a new row in the data cache.

RowDirtyStateNeeded

Used by the control to determine whether a row has any uncommitted changes.

CancelRowEdit

Used by the control to indicate that a row should revert to its cached values.

The following events are useful in virtual mode, but can be used regardless of the VirtualMode property setting.

Events Description

UserDeletingRow

UserDeletedRow

RowsRemoved

RowsAdded

Used by the control to indicate when rows are deleted or added, letting you update the data cache accordingly.

CellFormatting

CellParsing

CellValidating

CellValidated

RowValidating

RowValidated

Used by the control to format cell values for display and to parse and validate user input.

CellToolTipTextNeeded

Used by the control to retrieve cell ToolTip text when the DataSource property is set or the VirtualMode property is true.

Cell ToolTips are displayed only when the ShowCellToolTips property value is true.

CellErrorTextNeeded

RowErrorTextNeeded

Used by the control to retrieve cell or row error text when the DataSource property is set or the VirtualMode property is true.

Call the UpdateCellErrorText method or the UpdateRowErrorText method when you change the cell or row error text to ensure that the current value is displayed in the control.

Cell and row error glyphs are displayed when the ShowCellErrors and ShowRowErrors property values are true.

CellContextMenuStripNeeded

RowContextMenuStripNeeded

Used by the control to retrieve a cell or row ContextMenuStrip when the control DataSource property is set or the VirtualMode property is true.

RowHeightInfoNeeded

RowHeightInfoPushed

Used by the control to retrieve or store row height information in the data cache. Call the UpdateRowHeightInfo method when changing the cached row height information outside of a RowHeightInfoPushed event handler to ensure that the current value is used in the display of the control.

Best Practices in Virtual Mode

If you are implementing virtual mode in order to work efficiently with large amounts of data, you will also want to ensure that you are working efficiently with the DataGridView control itself. For more information about the efficient use of cell styles, automatic sizing, selections, and row sharing, see Best Practices for Scaling the Windows Forms DataGridView Control.

See Also

Tasks

Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control

Reference

DataGridView
VirtualMode

Concepts

Best Practices for Scaling the Windows Forms DataGridView Control
Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control

Other Resources

Performance Tuning in the Windows Forms DataGridView Control