#6
|
|||
|
|||
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 cell’s value to the integer value of 155 when 155 needs to be displayed it has to become formatted for the display. The cells and column’s 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\column’s 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 cell’s value to its formatted value. Retrieving the proper TypeConverter is based upon the cell’s 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 cell’s 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 cell’s FormattedValue, any padding or additional display and the borders. Painting the Display After the FormattedValue is retrieved the cell’s responsible for painting the cell’s 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). |