Share via


Best Practices for Scaling the Windows Forms DataGridView Control

The DataGridView control is designed to provide maximum scalability. 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. 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. Style information is stored in DataGridViewCellStyle objects. 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. Shortcut menus in the DataGridView control are represented by ContextMenuStrip controls. 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. For maximum scalability, use only the control's ContextMenuStrip property to specify a single shortcut menu for the entire control.

  • 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. 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:

For more information, see Sizing Options in the Windows Forms DataGridView Control.

Using the Selected Cells, Rows, and Columns Collections Efficiently

The SelectedCells collection does not perform efficiently with large selections. 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

Efficient memory use is achieved in the DataGridView control through shared rows. 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.

A row cannot be shared in an unbound DataGridView control if any of its cells contain values. 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:

In bound mode or virtual mode, you can provide ToolTips and shortcut menus for individual cells by handling the CellToolTipTextNeeded and CellContextMenuStripNeeded events.

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:

  • Avoid calling the Add(Object[]) overload of the Add method and the Insert(Object[]) overload of the Insert method of the System.Windows.Forms.DataGridView.Rows collection. These overloads automatically create unshared rows.

  • Be sure that the row specified in the System.Windows.Forms.DataGridView.RowTemplate property can be shared in the following cases:

  • Be sure that the row indicated by the indexSource parameter can be shared when calling the AddCopy, AddCopies, InsertCopy, and InsertCopies methods of the System.Windows.Forms.DataGridView.Rows collection.

  • Be sure that the specified row or rows can be shared when calling the Add(DataGridViewRow) overload of the Add method, the AddRange method, the Insert(Int32,DataGridViewRow) overload of the Insert method, and the InsertRange method of the System.Windows.Forms.DataGridView.Rows collection.

To determine whether a row is shared, use the System.Windows.Forms.DataGridViewRowCollection.SharedRow(System.Int32) method to retrieve the row object, and then check the object's Index property. 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. 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:

  • Avoid indexing the Rows collection or iterating through it with a foreach loop. You will not typically need to access rows directly. DataGridView methods that operate on rows take row index arguments rather than row instances. Additionally, handlers for row-related events receive event argument objects with row properties that you can use to manipulate rows without causing them to become unshared.

  • If you need to access a row object, use the System.Windows.Forms.DataGridViewRowCollection.SharedRow(System.Int32) method and pass in the row's actual index. Note, however, that modifying a shared row object retrieved through this method will modify all the rows that share this object. The row for new records is not shared with other rows, however, so it will not be affected when you modify any other row. Note also that different rows represented by a shared row may have different shortcut menus. To retrieve the correct shortcut menu from a shared row instance, use the GetContextMenuStrip method and pass in the row's actual index. If you access the shared row's ContextMenuStrip property instead, it will use the shared row index of -1 and will not retrieve the correct shortcut menu.

  • Avoid indexing the System.Windows.Forms.DataGridViewRow.Cells collection. Accessing a cell directly will cause its parent row to become unshared, instantiating a new DataGridViewRow. Handlers for cell-related events receive event argument objects with cell properties that you can use to manipulate cells without causing rows to become unshared. You can also use the CurrentCellAddress property to retrieve the row and column indexes of the current cell without accessing the cell directly.

  • Avoid cell-based selection modes. These modes cause rows to become unshared. Instead, set the System.Windows.Forms.DataGridView.SelectionMode property to System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect or System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect.

  • Do not handle the System.Windows.Forms.DataGridViewRowCollection.CollectionChanged or System.Windows.Forms.DataGridView.RowStateChanged events. These events cause rows to become unshared. Also, do not call the System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs) or System.Windows.Forms.DataGridView.OnRowStateChanged(System.Int32,System.Windows.Forms.DataGridViewRowStateChangedEventArgs) methods, which raise these events.

  • Do not access the System.Windows.Forms.DataGridView.SelectedCells collection when the System.Windows.Forms.DataGridView.SelectionMode property value is FullColumnSelect, ColumnHeaderSelect, FullRowSelect, or RowHeaderSelect. This causes all selected rows to become unshared.

  • Do not call the System.Windows.Forms.DataGridView.AreAllCellsSelected(System.Boolean) method. This method can cause rows to become unshared.

  • Do not call the System.Windows.Forms.DataGridView.SelectAll method when the System.Windows.Forms.DataGridView.SelectionMode property value is CellSelect. This causes all rows to become unshared.

  • Do not set the ReadOnly or Selected property of a cell to false when the corresponding property in its column is set to true. This causes all rows to become unshared.

  • Do not access the System.Windows.Forms.DataGridViewRowCollection.List property. This causes all rows to become unshared.

  • Do not call the Sort(IComparer) overload of the Sort method. Sorting with a custom comparer causes all rows to become unshared.

See Also

Tasks

How to: Set Default Cell Styles for the Windows Forms DataGridView Control

Reference

DataGridView

Concepts

Virtual Mode in the Windows Forms DataGridView Control
Data Display Modes in the Windows Forms DataGridView Control
Cell Styles in the Windows Forms DataGridView Control
Sizing Options in the Windows Forms DataGridView Control

Other Resources

Performance Tuning in the Windows Forms DataGridView Control