Column Types in the Windows Forms DataGridView Control
Thecontrol uses several column types to display its information and enable users to modify or add information.
When you bind a DataGridView control and set theproperty to true, columns are automatically generated using default column types appropriate for the data types contained in the bound data source.
You can also create instances of any of the column classes yourself and add them to the collection returned by theproperty. You can create these instances for use as unbound columns, or you can manually bind them. Manually bound columns are useful, for example, when you want to replace an automatically generated column of one type with a column of another type.
The following table describes the various column classes available for use in the DataGridView control.
Used with text-based values. Generated automatically when binding to numbers and strings.
Used withand values. Generated automatically when binding to values of these types.
Used to display images. Generated automatically when binding to byte arrays,objects, or objects.
Used to display buttons in cells. Not automatically generated when binding. Typically used as unbound columns.
Used to display drop-down lists in cells. Not automatically generated when binding. Typically data-bound manually.
Used to display links in cells. Not automatically generated when binding. Typically data-bound manually.
Your custom column type
You can create your own column class by inheriting theclass or any of its derived classes to provide custom appearance, behavior, or hosted controls. For more information, see
These column types are described in more detail in the following sections.
The DataGridViewTextBoxColumn is a general-purpose column type for use with text-based values such as numbers and strings. In editing mode, acontrol is displayed in the active cell, enabling users to modify the cell value.
Cell values are automatically converted to strings for display. Values entered or modified by the user are automatically parsed to create a cell value of the appropriate data type. You can customize these conversions by handling theand events of the DataGridView control.
The cell value data type of a column is specified in theproperty of the column.
The DataGridViewCheckBoxColumn is used with Boolean and CheckState values. Boolean values display as two-state or three-state check boxes, depending on the value of theproperty. When the column is bound to CheckState values, the ThreeState property value is true by default.
Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle theevent, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the event to respond to it. To commit the change when the cell is clicked, you must handle the event. In the handler, if the current cell is a check box cell, call the method and pass in the value.
The DataGridViewImageColumn is used to display images. Image columns can be populated automatically from a data source, populated manually for unbound columns, or populated dynamically in a handler for the CellFormatting event.
The automatic population of an image column from a data source works with byte arrays in a variety of image formats, including all formats supported by the Image class and the OLE Picture format used by Microsoft® Access and the Northwind sample database.
Populating an image column manually is useful when you want to provide the functionality of a DataGridViewButtonColumn, but with a customized appearance. You can handle the System.Windows.Forms.DataGridView.CellClick event to respond to clicks within an image cell.
Populating the cells of an image column in a handler for the CellFormatting event is useful when you want to provide images for calculated values or values in non-image formats. For example, you may have a "Risk" column with string values such as "high", "middle", and "low" that you want to display as icons. Alternately, you may have an "Image" column that contains the locations of images that must be loaded rather than the binary content of the images.
With the DataGridViewButtonColumn, you can display a column of cells that contain buttons. This is useful when you want to provide an easy way for your users to perform actions on particular records, such as placing an order or displaying child records in a separate window.
Button columns are not generated automatically when data-binding a DataGridView control. To use button columns, you must create them manually and add them to the collection returned by the System.Windows.Forms.DataGridView.Columns property.
You can respond to user clicks in button cells by handling the System.Windows.Forms.DataGridView.CellClick event.
With the DataGridViewComboBoxColumn, you can display a column of cells that contain drop-down list boxes. This is useful for data entry in fields that can only contain particular values, such as the Category column of the Products table in the Northwind sample database.
You can populate the drop-down list used for all cells the same way you would populate adrop-down list, either manually through the collection returned by the property, or by binding it to a data source through the , , and properties. For more information, see .
You can bind the actual cell values to the data source used by the DataGridView control by setting theproperty of the System.Windows.Forms.DataGridViewComboBoxColumn.
Combo box columns are not generated automatically when data-binding a DataGridView control. To use combo box columns, you must create them manually and add them to the collection returned by the Columns property.
With the DataGridViewLinkColumn, you can display a column of cells that contain hyperlinks. This is useful for URL values in the data source or as an alternative to the button column for special behaviors such as opening a window with child records.
Link columns are not generated automatically when data-binding a DataGridView control. To use link columns, you must create them manually and add them to the collection returned by the Columns property.
You can respond to user clicks on links by handling theevent. This event is distinct from the CellClick and events, which occur when a user clicks anywhere in a cell.
The DataGridViewLinkColumn class provides several properties for modifying the appearance of links before, during, and after they are clicked.