Export (0) Print
Expand All

DataGridViewCellFormattingEventArgs Constructor

Initializes a new instance of the DataGridViewCellFormattingEventArgs class.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public DataGridViewCellFormattingEventArgs(
	int columnIndex,
	int rowIndex,
	Object value,
	Type desiredType,
	DataGridViewCellStyle cellStyle


Type: System.Int32

The column index of the cell that caused the event.

Type: System.Int32

The row index of the cell that caused the event.

Type: System.Object

The cell's contents.

Type: System.Type

The type to convert value to.

Type: System.Windows.Forms.DataGridViewCellStyle

The style of the cell that caused the event.


columnIndex is less than -1


rowIndex is less than -1.

The desiredType parameter represents the type that the value parameter should be converted to, and desiredType is assigned the cell's FormattedValueType property. For example, if a cell is formatting picture names as bitmaps, value is the String that contains the picture name, and desiredType is a Type representing the Bitmap type.

If the CellFormatting event handler does not set the Value property to a type that can be displayed by the cell, the cell contents will be formatted using the Format, NullValue, and FormatProvider properties.

The following code example demonstrates how to use a DataGridViewCellFormattingEventArgs.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    // If the column is the Artist column, check the 
    // value. 
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
        if (e.Value != null)
            // Check for the string "pink" in the cell.
            string stringValue = (string)e.Value;
            stringValue = stringValue.ToLower();
            if ((stringValue.IndexOf("pink") > -1))
                e.CellStyle.BackColor = Color.Pink;

    else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")

//Even though the date internaly stores the year as YYYY, using formatting, the 
//UI can have the format in YY.   
private static void ShortFormDateFormat(DataGridViewCellFormattingEventArgs formatting)
    if (formatting.Value != null)
            System.Text.StringBuilder dateString = new System.Text.StringBuilder();
            DateTime theDate = DateTime.Parse(formatting.Value.ToString());

            formatting.Value = dateString.ToString();
            formatting.FormattingApplied = true;
        catch (FormatException)
            // Set to false in case there are other handlers interested trying to 
            // format this DataGridViewCellFormattingEventArgs instance.
            formatting.FormattingApplied = false;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft