縮放 Windows Form 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 Forms 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 Forms 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