Compartir a través de


DataGridView Control Architecture (Windows Forms)

The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data. These classes are all contained in the System.Windows.Forms namespace, and they are all named with the "DataGridView" prefix.

Architecture Elements

The primary DataGridView companion classes derive from DataGridViewElement. The following object model illustrates the DataGridViewElement inheritance hierarchy.


DataGridViewElement object model

DataGridViewElement Object Model

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 following sections describe the DataGridView companion classes in more detail.

DataGridViewElementStates

The DataGridViewElementStates enumeration contains the following values:

The values of this enumeration can be combined with the bitwise logical operators, so the State property can express more than one state at once. For example, a DataGridViewElement can be simultaneously Frozen, Selected, and Visible.

Cells and Bands

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.

DataGridViewCell

The 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 following object model illustrates this usage and shows the DataGridViewCell inheritance hierarchy.


DataGridViewCell object model

DataGridViewCell Object Model

The DataGridViewCell type is an abstract base class, from which all cell types derive. DataGridViewCell and its derived types are not Windows 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.

The following list shows the classes derived from DataGridViewCell:

DataGridViewColumn

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. The following object model illustrates this usage and shows the DataGridViewColumn inheritance hierarchy.


DataGridViewColumn object model

DataGridViewColumn Object Model

Some of the key cell types have corresponding column types. These are derived from the DataGridViewColumn base class.

The following list shows the classes derived from DataGridViewColumn:

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 object model illustrates the usage of these controls.


DataGridView editing control object model

DataGridView Editing Control Object Model

The following editing controls are provided with the DataGridView control:

For information about creating your own editing controls, see How to: Host Controls in Windows Forms DataGridView Cells.

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. The following object model illustrates this usage and shows the DataGridViewRow inheritance hierarchy.


DataGridViewRow object model

DataGridViewRow Object Model

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.

See Also

Reference

DataGridView Control Overview (Windows Forms)

Concepts

Using the Row for New Records in the Windows Forms DataGridView Control

Other Resources

Customizing the Windows Forms DataGridView Control