缩放 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. 尽管与典型的 DataGridView 控件中的单元格相比,其行比典型的控件中的单元数少很多,而且还有比行少的列,但 SelectedRowsSelectedColumns 集合也可能效率低下。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.

每当向 DataGridViewRowCollection添加行时,DataGridView 控件将自动尝试使用共享行。The 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