System.Windows.Forms.DataGridViewCellFormattingEventArgs Class

Provides data for the DataGridView.CellFormatting event of a System.Windows.Forms.DataGridView.

See Also: DataGridViewCellFormattingEventArgs Members


public class DataGridViewCellFormattingEventArgs : ConvertEventArgs


Handle the DataGridView.CellFormatting event to customize the conversion of a cell value into a format suitable for display or to customize the appearance of a cell depending on its state or value.

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