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.
| Exception | Condition |
|---|---|
| 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; } } }
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.