Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
DataGridViewCellFormattingEventArgs Constructor

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
)

Parameters

columnIndex
Type: System.Int32

The column index of the cell that caused the event.

rowIndex
Type: System.Int32

The row index of the cell that caused the event.

value
Type: System.Object

The cell's contents.

desiredType
Type: System.Type

The type to convert value to.

cellStyle
Type: System.Windows.Forms.DataGridViewCellStyle

The style of the cell that caused the event.

ExceptionCondition
ArgumentOutOfRangeException

columnIndex is less than -1

-or-

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")
    {
        ShortFormDateFormat(e);
    }
}

//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)
    {
        try
        {
            System.Text.StringBuilder dateString = new System.Text.StringBuilder();
            DateTime theDate = DateTime.Parse(formatting.Value.ToString());

            dateString.Append(theDate.Month);
            dateString.Append("/");
            dateString.Append(theDate.Day);
            dateString.Append("/");
            dateString.Append(theDate.Year.ToString().Substring(2));
            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
Show:
© 2015 Microsoft