dotnetmonitor.com

 
Index
Previous
Next

 

An important part of understanding the structure of the DataGridView is to understand how a DataGridViewCell works.

 

A Cells 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 doesnt know or keep the value is at all. Anytime the cells value is needed the grid goes to the datasource and looks up the value for the column and row and returns that as the cells value. In virtual mode this routine is very similar except the grid raises the CellValueNeeded event to get the cells value. At the cell level, all of this is controlled via the DataGridViewCell::GetValue(...) method.

The data type for the cells Value property by default is of type object. When a column becomes databound its ValueType property is set which causes each cells 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 cells value to the integer value of 155 when 155 needs to be displayed it has to become formatted for the display. The cells and columns 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\columns 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 cells value to its formatted value. Retrieving the proper TypeConverter is based upon the cells 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 cells 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 cells FormattedValue, any padding or additional display and the borders.

Painting the Display

After the FormattedValue is retrieved the cells responsible for painting the cells 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 cells value. One important thing to note is that the user in reality is editing the cells FormattedValue. When committing the value the FormattedValue has to be converted back to the cells 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 cells formatted value is parsed.

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

What is the DataGridView
Differences between the DataGridView and DataGrid controls
Highlight of features
Structure of DGV
Architecture Elements
Cells and Bands