Export (0) Print
Expand All
Expand Minimize

DataGridViewCellParsingEventHandler Delegate

Represents the method that will handle a CellParsing event of a DataGridView.

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

public delegate void DataGridViewCellParsingEventHandler(
	Object^ sender, 
	DataGridViewCellParsingEventArgs^ e
)

Parameters

sender
Type: System::Object

The source of the event.

e
Type: System.Windows.Forms::DataGridViewCellParsingEventArgs

A DataGridViewCellParsingEventArgs that contains the event data.

Handle the CellParsing event to provide custom value conversion from a user-specified value to a value in the type specified by the cell ValueType property.

When you handle the CellParsing event, you can convert the value yourself or you can customize the default conversion. For example, you can convert the value yourself using the cell ParseFormattedValue method with type converters of your choosing. Alternatively, you can let the default type converters parse the value, but modify the NullValue, DataSourceNullValue, and FormatProvider properties of the object returned by the DataGridViewCellParsingEventArgs::InheritedCellStyle property, which is initialized using the cell InheritedStyle property.

When you convert the value yourself, replace the initial, formatted value of the ConvertEventArgs::Value property with the converted value in the type specified by the cell ValueType property. To indicate that no further parsing is necessary, set the DataGridViewCellParsingEventArgs::ParsingApplied property to true.

When the event handler completes, if the Value is nullptr or is not of the correct type, or the ParsingApplied property is false, the Value is parsed using the cell ParseFormattedValue method with default type converters. The default implementation of this method parses the value using the NullValue, DataSourceNullValue, and FormatProvider properties of the cell style passed in. If the value is not equal to NullValue, the value is parsed using the FormatProvider property and the type converters passed in.

To customize the conversion of a cell value into a formatted value for display, handle the CellFormatting event.

For more information about handling events, see Consuming Events.

When you create a DataGridViewCellParsingEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

The following code example demonstrates using DataGridViewCellParsingEventHandler to check the validity of date entries.

// Handling CellParsing allows one to accept user input, then map it to a different 
// internal representation. 
void dataGridView1_CellParsing( Object^ /*sender*/, DataGridViewCellParsingEventArgs^ e )
{
   if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Release Date" ) )
   {
      if ( e != nullptr )
      {
         if ( e->Value != nullptr )
         {
            try
            {
               // Map what the user typed into UTC.
               e->Value = DateTime::Parse( e->Value->ToString() ).ToUniversalTime();

               // Set the ParsingApplied property to  
               // Show the event is handled.
               e->ParsingApplied = true;
            }
            catch ( FormatException^ /*ex*/ ) 
            {
               // Set to false in case another CellParsing handler 
               // wants to try to parse this DataGridViewCellParsingEventArgs instance.
               e->ParsingApplied = false;
            }
         }
      }
   }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Show:
© 2014 Microsoft