Thread: GridView
View Single Post
Old 06-01-2009, 04:59 PM
Posts: n/a

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

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

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).
Reply With Quote