Column Types in the Windows Forms DataGridView Control
The DataGridView control uses several column types to display its information and enable users to modify or add information.
When you bind a DataGridView control and set the AutoGenerateColumns property 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 the Columns property. 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.
|DataGridViewTextBoxColumn||Used with text-based values. Generated automatically when binding to numbers and strings.|
|DataGridViewCheckBoxColumn||Used with Boolean and CheckState values. Generated automatically when binding to values of these types.|
|DataGridViewImageColumn||Used to display images. Generated automatically when binding to byte arrays, Image objects, or Icon objects.|
|DataGridViewButtonColumn||Used to display buttons in cells. Not automatically generated when binding. Typically used as unbound columns.|
|DataGridViewComboBoxColumn||Used to display drop-down lists in cells. Not automatically generated when binding. Typically data-bound manually.|
|DataGridViewLinkColumn||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 the DataGridViewColumn class or any of its derived classes to provide custom appearance, behavior, or hosted controls. For more information, see How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance|
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, a TextBox control 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 the CellFormatting and CellParsing events of the DataGridView control.
The cell value data type of a column is specified in the ValueType property 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 the ThreeState property. 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 the CellClick event, 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 CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the CommitEdit method and pass in the Commit 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 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
"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 DataGridView.Columns property.
You can respond to user clicks in button cells by handling the 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 a ComboBox drop-down list, either manually through the collection returned by the Items property, or by binding it to a data source through the DataSource, DisplayMember, and ValueMember properties. For more information, see ComboBox Control.
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.
The DataGridViewLinkColumn class provides several properties for modifying the appearance of links before, during, and after they are clicked.
- DataGridView Control
- How to: Display Images in Cells of the Windows Forms DataGridView Control
- How to: Work with Image Columns in the Windows Forms DataGridView Control
- Customizing the Windows Forms DataGridView Control