Windows 窗体 DataGridView 控件中的虚拟模式Virtual Mode in the Windows Forms DataGridView Control

虚拟模式下,你可以管理之间的交互DataGridView控件和自定义数据缓存。With virtual mode, you can manage the interaction between the DataGridView control and a custom data cache. 若要实现虚拟模式下,设置VirtualMode属性设置为true并处理一个或多个本主题中所述的事件。To implement virtual mode, set the VirtualMode property to true and handle one or more of the events described in this topic. 通常会处理至少CellValueNeeded事件,它使数据缓存中的值查找的控件。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. 绑定模式下,在您设置DataSource属性和控件自动从指定的源加载数据,并提交用户更改返回到它。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. 这有时称为"混合的模式",并可用于显示某些操作,如计算出的值或用户界面 (UI) 控件。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. 因此,如果启用排序在混合模式下,您必须管理在本地缓存中的未绑定的数据并实现虚拟模式以使DataGridView控件与之进行交互。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.

有关使用虚拟模式下维护未绑定列中的值的详细信息,请参阅中的示例DataGridViewCheckBoxColumn.ThreeState属性和System.Windows.Forms.DataGridViewComboBoxColumn类参考主题。For more information about using virtual mode to maintain the values in unbound columns, see the examples in the 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. 此方案中使用大量的数据通过慢速网络连接或具有有限的数量的 RAM 或存储空间的客户端计算机时特别有用。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.

有关在实时方案中使用的虚拟模式的详细信息,请参阅实时数据加载 Windows 窗体 DataGridView 控件中实现虚拟模式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

如果你的数据是只读的CellValueNeeded事件可能是您将需要处理的唯一事件。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.

一些标准DataGridView事件 (如用户添加或删除行,或单元格时的值时发生的事件进行编辑、 分析、 验证,或格式) 均处于虚拟模式非常有用。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.

有关实现虚拟模式来管理具有行级别的提交作用域的读/写数据的详细信息,请参阅演练:在 Windows 中实现虚拟模式窗体 DataGridView 控件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.

实现虚拟模式的单元格级提交作用域的示例,请参阅VirtualMode属性参考主题。For an example that implements virtual mode with a cell-level commit scope, see the VirtualMode property reference topic.

仅将发生以下事件时VirtualMode属性设置为trueThe following events occur only when the VirtualMode property is set to true.

EventEvent 描述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.

调用UpdateCellValue方法时更改缓存的值之外的CellValuePushed事件处理程序以确保在控件中显示的当前值并应用当前有效的任何自动调整大小模式。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.

以下事件可在虚拟模式下,但可以使用而不考虑VirtualMode属性设置。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 该控件用于检索单元格工具提示文本时DataSource属性设置或VirtualMode属性是trueUsed by the control to retrieve cell ToolTip text when the DataSource property is set or the VirtualMode property is true.

显示单元格工具提示时,才ShowCellToolTips属性值是trueCell ToolTips are displayed only when the ShowCellToolTips property value is true.
CellErrorTextNeeded

RowErrorTextNeeded
该控件用于检索单元格或行错误文本时DataSource属性设置或VirtualMode属性是trueUsed by the control to retrieve cell or row error text when the DataSource property is set or the VirtualMode property is true.

调用UpdateCellErrorText方法或UpdateRowErrorText方法时更改单元格或行错误文本,以确保在控件中显示的当前值。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.

显示单元格和行错误标志符号时ShowCellErrorsShowRowErrors属性的值为trueCell and row error glyphs are displayed when the ShowCellErrors and ShowRowErrors property values are true.
CellContextMenuStripNeeded

RowContextMenuStripNeeded
该控件用于检索单元格或行ContextMenuStrip时该控件DataSource属性设置或VirtualMode属性是trueUsed 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. 调用UpdateRowHeightInfo方法时更改之外的缓存的行高度信息RowHeightInfoPushed事件处理程序以确保在控件的显示中使用的当前值。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

为了有效地使用大量的数据,要实现虚拟模式下,如果您还需要确保与你也会有效地工作DataGridView控件本身。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. 有关有效地使用的单元格样式、 自动调整大小、 选择以及行共享的详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法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