System.Windows.Forms.DataGridView.CellFormatting Event

Occurs when the contents of a cell need to be formatted for display.


public event DataGridViewCellFormattingEventHandler CellFormatting


By default, the System.Windows.Forms.DataGridView control will attempt to convert a cell's value into a format suitable for display. For example, it will convert a numerical value into a string for display in a text box cell. You can indicate the formatting convention to use by setting the DataGridViewCellStyle.Format property of the System.Windows.Forms.DataGridViewCellStyle returned by properties such as the DataGridView.DefaultCellStyle property.

If the standard formatting is insufficient, you can customize the formatting by handling the DataGridView.CellFormatting event. This event lets you indicate the exact display value as well as the cell styles, such as background and foreground color, to use for the cell display. This means you can handle this event for any kind of cell formatting, regardless of whether the cell value itself needs formatting.

The DataGridView.CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event. This event also occurs when the cell DataGridViewCell.FormattedValue is retrieved or its DataGridViewCell.GetFormattedValue(object, int, DataGridViewCellStyle@, System.ComponentModel.TypeConverter, System.ComponentModel.TypeConverter, DataGridViewDataErrorContexts) method is called.

When you handle the DataGridView.CellFormatting event, the ConvertEventArgs.Value property is initialized with the cell value. If you provide custom conversion from the cell value to the display value, set the ConvertEventArgs.Value property to the converted value, ensuring that the new value is of the type specified by the cell DataGridViewCell.FormattedValueType property. To indicate that no further value formatting is necessary, set the DataGridViewCellFormattingEventArgs.FormattingApplied property to true.

When the event handler completes, if the ConvertEventArgs.Value is null or is not of the correct type, or the DataGridViewCellFormattingEventArgs.FormattingApplied property is false, the ConvertEventArgs.Value is formatted using the DataGridViewCellStyle.Format, DataGridViewCellStyle.NullValue, DataGridViewCellStyle.DataSourceNullValue, and DataGridViewCellStyle.FormatProvider properties of the cell style returned by the DataGridViewCellFormattingEventArgs.CellStyle property, which is initialized using the cell DataGridViewCell.InheritedStyle property.

Regardless of the value of the DataGridViewCellFormattingEventArgs.FormattingApplied property, the display properties of the object returned by the DataGridViewCellFormattingEventArgs.CellStyle property are used to render the cell.

For more information about custom formatting using the DataGridView.CellFormatting event, see How to: Customize Data Formatting in the Windows Forms DataGridView Control.

To avoid performance penalties when handling this event, access the cell through the parameters of the event handler rather than accessing the cell directly.

To customize the conversion of a formatted, user-specified value into an actual cell value, handle the DataGridView.CellParsing event.

For more information about handling events, see Consuming Events.


Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions:
Since: .NET 2.0