您的位置:首页 > 编程语言 > C#

Best Practices for Scaling the Windows Forms DataGridView Control(winform中控制DataGridView控件规模的最佳实践)

2013-04-27 16:05 1126 查看
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:

Avoid setting cell style properties for individual DataGridViewCell or DataGridViewRow objects.
This includes the row object specified by theRowTemplate property. Each new row that is
cloned from the row template will receive its own copy of the template's cell style object. For maximum scalability, set cell style properties at the DataGridView level.
For example, set the DataGridViewDefaultCellStyle property rather than
theDataGridViewCellStyle property.

If some cells require formatting other than default formatting, use the same DataGridViewCellStyle instance
across groups of cells, rows, or columns. Avoid directly setting properties of type DataGridViewCellStyle on
individual cells, rows, and columns. For an example of cell style sharing, see How to: Set Default Cell Styles for the Windows
Forms DataGridView Control. You can also avoid a performance penalty when setting cell styles individually by handling the CellFormatting event
handler. For an example, see How to: Customize Data Formatting in the Windows Forms DataGridView Control.

When determining a cell's style, use the DataGridViewCellInheritedStyle property
rather than the DataGridViewCellStyle property. Accessing the Styleproperty
creates a new instance of the DataGridViewCellStyle class if the property has
not already been used. Additionally, this object might not contain the complete style information for the cell if some styles are inherited from the row, column, or control. For more information about cell style inheritance, see Cell
Styles in the Windows Forms DataGridView Control.

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:

Avoid using automatic sizing on a DataGridView control with a large set of rows. If you
do use automatic sizing, only resize based on the displayed rows. Use only the displayed rows in virtual mode as well.

For rows and columns, use the DisplayedCells or DisplayedCellsExceptHeaders field
of the DataGridViewAutoSizeRowsMode,DataGridViewAutoSizeColumnsMode,
and DataGridViewAutoSizeColumnMode enumerations.

For row headers, use the AutoSizeToDisplayedHeaders or AutoSizeToFirstHeader field
of the DataGridViewRowHeadersWidthSizeModeenumeration.

For maximum scalability, turn off automatic sizing and use programmatic resizing.

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:

To determine whether all the cells in the DataGridView have been selected before you access
the contents of the SelectedCells collection, check the return value of
the AreAllCellsSelected method. Note, however, that this method can
cause rows to become unshared. For more information, see the next section.

Avoid using the Count property of the DataGridViewSelectedCellCollection to
determine the number of selected cells. Instead, use theDataGridViewGetCellCount method
and pass in the DataGridViewElementStatesSelected value. Similarly, use theDataGridViewRowCollectionGetRowCount and DataGridViewColumnCollectionGetColumnCount methods
to determine the number of selected elements, rather than accessing the selected row and column collections.

Avoid cell-based selection modes. Instead, set the DataGridViewSelectionMode property
to DataGridViewSelectionModeFullRowSelect orDataGridViewSelectionModeFullColumnSelect.

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:

The row contains a single selected cell that is not in a selected column.

The row contains a cell with its ToolTipText or ContextMenuStrip properties
set.

The row contains a DataGridViewComboBoxCell with its Items property
set.

In bound mode or virtual mode, you can provide ToolTips and shortcut menus for individual cells by handling the CellToolTipTextNeeded andCellContextMenuStripNeeded 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 DataGridViewRowscollection. These overloads automatically create unshared rows.

Be sure that the row specified in the DataGridViewRowTemplate property can
be shared in the following cases:

When calling the Add() or Add(Int32) overloads of the Add method
or the Insert(Int32,Int32) overload of the Insert method
of theDataGridViewRows collection.

When increasing the value of the DataGridViewRowCount property.

When setting the DataGridViewDataSource property.

Be sure that the row indicated by the indexSource parameter can be shared when calling the AddCopyAddCopiesInsertCopy,
and InsertCopiesmethods of the DataGridViewRows collection.

Be sure that the specified row or rows can be shared when calling the Add(DataGridViewRow) overload of the Add method,
the AddRangemethod, the Insert(Int32,DataGridViewRow) overload
of the Insert method, and the InsertRange method
of the DataGridViewRows collection.

To determine whether a row is shared, use the DataGridViewRowCollectionSharedRow method
to retrieve the row object, and then check the object's Indexproperty. 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. DataGridViewmethods 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 DataGridViewRowCollectionSharedRow 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 DataGridViewRowCells collection. Accessing a cell directly
will cause its parent row to become unshared, instantiating a newDataGridViewRow. 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 DataGridViewSelectionMode property
toDataGridViewSelectionModeFullRowSelect or DataGridViewSelectionModeFullColumnSelect.

Do not handle the DataGridViewRowCollectionCollectionChanged or DataGridViewRowStateChanged events.
These events cause rows to become unshared. Also, do not call the DataGridViewRowCollectionOnCollectionChanged or DataGridViewOnRowStateChanged methods,
which raise these events.

Do not access the DataGridViewSelectedCells collection when the DataGridViewSelectionMode property
value is FullColumnSelect,ColumnHeaderSelectFullRowSelect,
or RowHeaderSelect. This causes all selected rows to become unshared.

Do not call the DataGridViewAreAllCellsSelected method. This method
can cause rows to become unshared.

Do not call the DataGridViewSelectAll method when the DataGridViewSelectionMode 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 DataGridViewRowCollectionList 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.

原文:

http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐