Edit

Share via


DataGridViewCellParsingEventHandler Delegate

Definition

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

C#
public delegate void DataGridViewCellParsingEventHandler(object sender, DataGridViewCellParsingEventArgs e);
C#
public delegate void DataGridViewCellParsingEventHandler(object? sender, DataGridViewCellParsingEventArgs e);

Parameters

sender
Object

The source of the event.

Examples

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

C#
// Handling CellParsing allows one to accept user input, then map it to a different
// internal representation.
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
    {
        if (e != null)
        {
            if (e.Value != null)
            {
                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)
                {
                    // Set to false in case another CellParsing handler
                    // wants to try to parse this DataGridViewCellParsingEventArgs instance.
                    e.ParsingApplied = false;
                }
            }
        }
    }
}

Remarks

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 null 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 how to handle events, see Handling and Raising 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 Handling and Raising Events.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also