DataGridViewCell

This is the fundamental unit of interaction for the DataGridView
You can access cells by using the Cells collection of the DataGridViewRows class.

dgvDataGridViewRow.Cells(0) 

You can access the selected cells by using the SelectedCells collection

dgvDataGridViewRow.Cells(0) 

DataGridViewCell objects do not control their own appearance and painting features


Value

For a DataGridView that is not databound and not in virtual mode each cell stores its own value


Value (Databound)

For a DataGridView that is databound each cell does not know its actual value and must be looked up in the datasource.
The data type of each cell is automatically changed and this can be seen from the ValueType property.


FormattedValue

The FormattedValueType property determines the type that is used for display.
Most columns use String


Any time the FormattedValue is required the CellFormatting event is fired


If a cell cannot retrieve its formatted value correctly it raises the DataError event.


DataGridViewCellStyle

Always try and set the default cell style for the whole DataGridView control

dgvDataGridView.DefaultCellStyle = 

Dim highlightCellStyle As New DataGridViewCellStyle 
highlightCellStyle.BackColor = Color.Red

Dim currencyCellStyle As New DataGridViewCellStyle
currencyCellStyle.Format = "C"
currencyCellStyle.ForeColor = Color.Green

With Me.dataGridView1
    .DefaultCellStyle.BackColor = Color.Beige
    .DefaultCellStyle.Font = New Font("Tahoma", 12)
    .Rows(3).DefaultCellStyle = highlightCellStyle
    .Rows(8).DefaultCellStyle = highlightCellStyle
    .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
    .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
End With

The following are the list of objects and properties that can get or set DataGridViewCellStyle objects

DataGridView.DefaultCellStyle 
DataGridViewColumn.DefaultCellStyle
DataGridViewRow.DefaultCellStyle

DataGridView.RowsDefaultCellStyle 
DataGridView.AlternatingRowsDefaultCellStyle
DataGridView.RowHeadersDefaultCellStyle
DataGridView.ColumnHeadersDefaultCellStyle

DataGridViewCell.Style 

DataGridViewColumn.InheritedStyle 
DataGridViewRow.InheritedStyle
DataGridViewCell.InheritedStyle

Getting the value of a style automatically instantiates a new DataGridViewCellStyle object if the property has not been previously set.
To avoid creating these accidentally you should check first using the following properties:

DataGridViewColumn.HasDefaultCellStyle 
DataGridViewRow.HasDefaultCellStyle

DataGridViewCell.HasStyle 

DataGridViewTextBoxCell

This is a specialised type of DataGridViewCell that is used to display a single string of editable text.
When you derive from this class you need to overwrite the Clone method.


Properties

Alignment 
BackColor 
DataSourceNullValue 
Font 
ForeColor 
Format 
FormatProvider 
NullValue 
Padding 
SelectionBackColor 
SelectionForeColor 
WrapMode 

DataGridViewCheckBoxCell


Properties

ValueGets or sets the value associated with the cell
VisibleGets a value indicating if the cell is in a row or column that has been hidden
RowIndexGets the index of the cells parent row
ColumnIndexGets the column index for the cell

if (checkboxCell.Value == true) 
{
}



DataGridViewCellTemplate

Cell templates are instances of a DataGridViewCell derived class.


Making changes to the CellTemplate of a column will update the template and update all the cells in that column, refreshing the display if necessary.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext