缩放 Windows 窗体 DataGridView 控件的最佳做法Best Practices for Scaling the Windows Forms DataGridView Control

DataGridView控件旨在提供最大可伸缩性。The DataGridView control is designed to provide maximum scalability. 如果需要显示大量的数据,则应遵循本主题,以避免消耗大量的内存或降低响应能力的用户界面 (UI) 中所述的准则。If you need to display large amounts of data, you should follow the guidelines described in this topic to avoid consuming large amounts of memory or degrading the responsiveness of the user interface (UI). 本主题讨论下列问题:This topic discusses the following issues:

  • 有效地使用的单元格样式Using cell styles efficiently

  • 有效地使用快捷菜单Using shortcut menus efficiently

  • 使用自动调整大小高效Using automatic resizing efficiently

  • 有效使用所选的单元格、 行和列集合Using the selected cells, rows, and columns collections efficiently

  • 使用共享的行Using shared rows

  • 防止行成为非共享行Preventing rows from becoming unshared

如果你有特殊性能需求,可以实现虚拟模式并提供你自己的数据管理操作。If you have special performance needs, you can implement virtual mode and provide your own data management operations. 有关详细信息,请参阅Windows 窗体 DataGridView 控件中的数据显示模式For more information, see Data Display Modes in the Windows Forms DataGridView Control.

有效地使用的单元格样式Using Cell Styles Efficiently

每个单元格、 行和列可以具有其自己的样式信息。Each cell, row, and column can have its own style information. 样式的信息存储在DataGridViewCellStyle对象。Style information is stored in DataGridViewCellStyle objects. 创建多个独立单元格样式对象DataGridView元素可以是效率不高,尤其在使用大量的数据时。Creating cell style objects for many individual DataGridView elements can be inefficient, especially when working with large amounts of data. 若要避免对性能产生影响,请使用以下准则:To avoid a performance impact, use the following guidelines:

有效地使用快捷菜单Using Shortcut Menus Efficiently

每个单元格、 行和列可以具有其自己的快捷菜单。Each cell, row, and column can have its own shortcut menu. 中的快捷菜单DataGridView控制由ContextMenuStrip控件。Shortcut menus in the DataGridView control are represented by ContextMenuStrip controls. 只需与单元格样式对象一样,为许多单个创建快捷方式菜单DataGridView元素会对性能产生负面影响。Just as with cell style objects, creating shortcut menus for many individual DataGridView elements will negatively impact performance. 若要避免这种下降,请使用以下准则:To avoid this penalty, use the following guidelines:

  • 避免创建各个单元格和行的快捷菜单。Avoid creating shortcut menus for individual cells and rows. 这包括行模板,新行添加到控件时,会克隆以及其快捷菜单。This includes the row template, which is cloned along with its shortcut menu when new rows are added to the control. 最大可伸缩性,使用仅该控件的ContextMenuStrip属性可以指定整个控件的一个快捷方式菜单。For maximum scalability, use only the control's ContextMenuStrip property to specify a single shortcut menu for the entire control.

  • 如果您需要针对多个行或单元格的多个快捷菜单,处理CellContextMenuStripNeededRowContextMenuStripNeeded事件。If you require multiple shortcut menus for multiple rows or cells, handle the CellContextMenuStripNeeded or RowContextMenuStripNeeded events. 这些事件使您可以管理快捷方式菜单对象自己,从而可以优化性能。These events let you manage the shortcut menu objects yourself, allowing you to tune performance.

使用自动调整大小高效Using Automatic Resizing Efficiently

行、 列和标头可以自动调整大小作为单元格的内容更改,以便完整地显示单元格的全部内容。Rows, columns, and headers can be automatically resized as cell content changes so that the entire contents of cells are displayed without clipping. 更改大小调整模式还可以调整行、 列和标头。Changing sizing modes can also resize rows, columns, and headers. 若要确定正确的大小,DataGridView控件必须检查必须适应每个单元格的值。To determine the correct size, the DataGridView control must examine the value of each cell that it must accommodate. 当处理大型数据集,此分析会降低该控件的性能,自动调整大小发生时。When working with large data sets, this analysis can negatively impact the performance of the control when automatic resizing occurs. 若要避免性能损失,使用以下准则:To avoid performance penalties, use the following guidelines:

有关详细信息,请参阅Windows 窗体 DataGridView 控件中调整大小选项For more information, see Sizing Options in the Windows Forms DataGridView Control.

有效使用选定的单元格、 行和列集合Using the Selected Cells, Rows, and Columns Collections Efficiently

SelectedCells集合不会使用大型选择有效地执行。The SelectedCells collection does not perform efficiently with large selections. SelectedRowsSelectedColumns集合还可能效率很低,尽管到一定程度上因为有许多较少行,比在典型的单元格DataGridView控制和许多较少的列比行。The SelectedRows and SelectedColumns collections can also be inefficient, although to a lesser degree because there are many fewer rows than cells in a typical DataGridView control, and many fewer columns than rows. 若要避免性能损失,使用这些集合时,使用以下准则:To avoid performance penalties when working with these collections, use the following guidelines:

使用共享行Using Shared Rows

在实现高效的内存使用DataGridView控制通过共享行。Efficient memory use is achieved in the DataGridView control through shared rows. 行将通过共享的实例来共享尽可能多的信息有关其外观和行为可能DataGridViewRow类。Rows will share as much information about their appearance and behavior as possible by sharing instances of the DataGridViewRow class.

虽然共享行实例节省内存,行可以轻松地成为非共享行。While sharing row instances saves memory, rows can easily become unshared. 例如,每当用户交互直接与单元格时,其行变为非共享。For example, whenever a user interacts directly with a cell, its row becomes unshared. 由于不能避免此操作,此主题中的准则十分有用,仅当使用的数据量非常大并且仅当用户将与相对较小部分数据交互每次运行程序时。Because this cannot be avoided, the guidelines in this topic are useful only when working with very large amounts of data and only when users will interact with a relatively small part of the data each time your program is run.

不能共享行中未绑定DataGridView控制是否有任何其单元格包含值。A row cannot be shared in an unbound DataGridView control if any of its cells contain values. DataGridView控件绑定到外部数据源或外部控件而不是在单元格对象,允许要共享的行时实现虚拟模式并提供您自己的数据源时,存储单元格的值。When the DataGridView control is bound to an external data source or when you implement virtual mode and provide your own data source, the cell values are stored outside the control rather than in cell objects, allowing the rows to be shared.

如果可以从行的状态和包含的单元格的列的状态确定所有单元格的状态,可以仅共享行对象。A row object can only be shared if the state of all its cells can be determined from the state of the row and the states of the columns containing the cells. 如果您更改单元格的状态,以便从其行和列的状态可以不再推导,不能共享行。If you change the state of a cell so that it can no longer be deduced from the state of its row and column, the row cannot be shared.

例如,不能在任何以下情况下共享行:For example, a row cannot be shared in any of the following situations:

在绑定的模式或虚拟模式下,可以提供工具提示和快捷菜单为单个单元通过处理CellToolTipTextNeededCellContextMenuStripNeeded事件。In bound mode or virtual mode, you can provide ToolTips and shortcut menus for individual cells by handling the CellToolTipTextNeeded and CellContextMenuStripNeeded events.

DataGridView控件将自动尝试使用共享的行,只要行添加到DataGridViewRowCollectionThe DataGridView control will automatically attempt to use shared rows whenever rows are added to the DataGridViewRowCollection. 使用以下准则以确保共享行:Use the following guidelines to ensure that rows are shared:

若要确定是否共享行,请使用DataGridViewRowCollection.SharedRow方法以检索行对象,然后查看该对象的Index属性。To determine whether a row is shared, use the DataGridViewRowCollection.SharedRow method to retrieve the row object, and then check the object's Index property. 共享的行始终具有Index属性值为 – 1。Shared rows always have an Index property value of –1.

防止行成为非共享行Preventing Rows from Becoming Unshared

共享的行可以成为非共享行代码或用户操作的结果。Shared rows can become unshared as a result of code or user action. 若要避免对性能产生影响,应避免使行成为非共享行。To avoid a performance impact, you should avoid causing rows to become unshared. 应用程序在开发期间,可以处理RowUnshared事件以确定当行成为非共享行。During application development, you can handle the RowUnshared event to determine when rows become unshared. 调试行共享问题时,这很有用。This is useful when debugging row-sharing problems.

若要防止行成为非共享行,请使用以下准则:To prevent rows from becoming unshared, use the following guidelines:

请参阅See also