The primary
DataGridView companion classes derive from DataGridViewElement.The DataGridViewElement class provides a
reference to the parent DataGridView control and has a State property, which
holds a value that represents a combination of values from the
DataGridViewElementStates enumeration.
The DataGridView
control comprises two fundamental kinds of objects: cells and bands. All cells
derive from the DataGridViewCell base class. The two kinds of bands,
DataGridViewColumn and DataGridViewRow, both derive from the DataGridViewBand
base class.
The DataGridView
control interoperates with several classes, but the most commonly encountered
are DataGridViewCell, DataGridViewColumn, and DataGridViewRow.
DataGridViewCellThe cell is the fundamental unit of interaction for the DataGridView. Display is centered on cells, and data entry is often performed through cells. You can access cells by using the Cells collection of the DataGridViewRow class, and you can access the selected cells by using the SelectedCells collection of the DataGridView control.The DataGridViewCell type is an abstract base class, from which all cell types derive. DataGridViewCell and its derived types are not Windoyws Forms controls, but some host Windows Forms controls. Any editing functionality supported by a cell is typically handled by a hosted control.
DataGridViewCell
objects do not control their own appearance and painting features in the same
way as Windows Forms controls. Instead, the DataGridView is responsible for the
appearance of its DataGridViewCell objects. You can significantly affect the
appearance and behavior of cells by interacting with the DataGridView control's
properties and events. When you have special requirements for customizations
that are beyond the capabilities of the DataGridView control, you can implement
your own class that derives from DataGridViewCell or one of its child classes.
An important
part of understanding the structure of the DataGridView is to understand how a
DataGridViewCell works.
A Cell’s Value
At the root of a
cell is its value. For cells in a column that is not databound and the grid is
not in virtual mode the cells actually store the value in the cell instance.
For databound cells the cell doesn’t “know” or keep the value is at all.
Anytime the cell’s value is needed the grid goes to the datasource and looks up
the value for the column and row and returns that as the cell’s value. In
virtual mode this routine is very similar except the grid raises the
CellValueNeeded event to get the cell’s value. At the cell level, all of this
is controlled via the DataGridViewCell::GetValue(...) method.
The data type
for the cell’s Value property by default is of type object. When a column
becomes databound its ValueType property is set which causes each cell’s
ValueType to be updated. The value of the ValueType property is important for
formatting.
Formatting for Display
Anytime the grid
needs to know “how would this cell display” it needs to get its FormattedValue. This is a complex
routine because formatting something on the screen usually needs to be
converted to a string. For example, although you set a cell’s value to the
integer value of 155 when 155 needs to be displayed it has to become formatted
for the display. The cells and column’s FormattedValueType property determines
the type that is used for display. Most columns use string, but the image and
check box cells\columns have different values. The DataGridViewImageCell and
column use Image as the default FormattedValueType since its painting code
knows how to display an image. A checkbox cell\column’s FormattedValueType
varies depending upon the value of ThreeState. At the cell level, all of this
is controlled via the DataGridViewCell::GetFormattedValue(...) method.
By default, the
DataGridView uses TypeConverters to convert a cell’s value to its formatted
value. Retrieving the proper TypeConverter is based upon the cell’s ValueType
and FormattedValueType properties.
For a cell, the
FormattedValue is requested many times. Anytime the cell is painted or when a
column needs to be autosized based upon the cell’s content; the FormattedValue
is even needed when determining if the mouse is over the cell content or not.
Anytime the FormattedValue is required the DataGridView raises the
CellFormatting event. This provides you with the opportunity to modify how the
cell is formatted.
If a cell cannot
retrieve its formatted value correctly it raises the DataError event.
Part of
formatting a cell for display is understanding what the preferred size of the
cell is. The preferred size is a combination of the cell’s FormattedValue, any
padding or additional display and the borders.
Painting the Display
After the
FormattedValue is retrieved the cell’s responsible for painting the cell’s
content. The cell determines the correct style to paint with (see the Styling
section later in this document) and paints the cell. It is important to note
that if a cell does not paint itself then nothing is painted. A row or column
performs no painting, so ensure that at least a background is painted in the
cell otherwise the rectangle remains invalidated (unpainted).
Parsing the Display
After the user
interacts with a cell at some point the user will edit a cell’s value. One
important thing to note is that the user in reality is editing the cell’s
FormattedValue. When committing the value the FormattedValue has to be converted
back to the cell’s value. This is called parsing.
At the cell level, all of this is controlled via the DataGridViewCell:: ParseFormattedValue
(int rowIndex) method.
By default,
TypeConverters are used again to parse the formatted value to the real value.
The DataGridView raises the CellParsing event at this time to provide you with
the opportunity to modify how the cell’s formatted value is parsed.
If a cell cannot
correctly parse the formatted value it raises the DataError event.
The schema of the DataGridView control's attached data store is expressed
in the DataGridView control's columns. You can access the DataGridView
control's columns by using the Columns collection. You can access the selected
columns by using the SelectedColumns collection.Some of the key cell types have corresponding
column types. These are derived from the DataGridViewColumn base class.
DataGridView Editing Controls
Cells that support advanced editing functionality typically use a hosted control that is derived from a Windows Forms control. These controls also implement the IDataGridViewEditingControl interface.
The following
table illustrates the relationship among cell types, column types, and editing
controls.
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
<><>
Cell type
|
Hosted control
|
Column type
|
DataGridViewButtonCell
|
n/a
|
DataGridViewButtonColumn
|
DataGridViewCheckBoxCell
|
n/a
|
DataGridViewCheckBoxColumn
|
DataGridViewComboBoxCell
|
DataGridViewComboBoxEditingControl
|
DataGridViewComboBoxColumn
|
DataGridViewImageCell
|
n/a
|
DataGridViewImageColumn
|
DataGridViewLinkCell
|
n/a
|
DataGridViewLinkColumn
|
DataGridViewTextBoxCell
|
DataGridViewTextBoxEditingControl
|
DataGridViewTextBoxColumn
|
DataGridViewRow
The DataGridViewRow class displays a record's data fields from the
data store to which the DataGridView control is attached. You can access the
DataGridView control's rows by using the Rows collection. You can access the
selected rows by using the SelectedRows collection.
You can derive
your own types from the DataGridViewRow class, although this will typically not
be necessary. The DataGridView control has several row-related events and
properties for customizing the behavior of its DataGridViewRow objects.
If you enable
the DataGridView control's AllowUserToAddRows property, a special row for adding
new rows appears as the last row. This row is part of the Rows collection, but
it has special functionality that may require your attention. For more
information, see Using the Row for New Records in the Windows Forms
DataGridView Control.
No comments:
Post a Comment