DataGridViewCellFormattingEventArgs Constructor (Int32, Int32, Object^, Type^, DataGridViewCellStyle^)
Initializes a new instance of the DataGridViewCellFormattingEventArgs class.
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.
void dataGridView1_CellFormatting( Object^ /*sender*/, DataGridViewCellFormattingEventArgs^ e ) { // If the column is the Artist column, check the // value. if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Artist" ) ) { if ( e->Value != nullptr ) { // Check for the string "pink" in the cell. String^ stringValue = dynamic_cast<String^>(e->Value); stringValue = stringValue->ToLower(); if ( (stringValue->IndexOf( "pink" ) > -1) ) { DataGridViewCellStyle^ pinkStyle = gcnew DataGridViewCellStyle; //Change the style of the cell. pinkStyle->BackColor = Color::Pink; pinkStyle->ForeColor = Color::Black; pinkStyle->Font = gcnew System::Drawing::Font( "Times New Roman",8,FontStyle::Bold ); e->CellStyle = pinkStyle; } } } else if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Release Date" ) ) { ShortFormDateFormat( e ); } } //Even though the date internaly stores the year as YYYY, using formatting, the //UI can have the format in YY. void ShortFormDateFormat( DataGridViewCellFormattingEventArgs^ formatting ) { if ( formatting->Value != nullptr ) { try { System::Text::StringBuilder^ dateString = gcnew 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 ( Exception^ /*notInDateFormat*/ ) { // Set to false in case there are other handlers interested trying to // format this DataGridViewCellFormattingEventArgs instance. formatting->FormattingApplied = false; } } }
Available since 2.0
DataGridView
DataGridView::CellFormatting
DataGridView::OnCellFormatting
DataGridViewCellStyle
DataGridViewCell::InheritedStyle
DataGridViewCell::Value
DataGridViewCell::FormattedValue
DataGridViewCell::FormattedValueType
CellStyle
ConvertEventArgs::Value
DataGridViewCellFormattingEventArgs Class
System.Windows.Forms Namespace
Cell Styles in the Windows Forms DataGridView Control