DataGridViewCellFormattingEventArgs Constructor
Collapse the table of content
Expand the table of content

DataGridViewCellFormattingEventArgs Constructor (Int32, Int32, Object, Type, DataGridViewCellStyle)


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.

Exception Condition

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
Available since 2.0
Return to top
© 2016 Microsoft