DataGridView.CellParsing Event
Assembly: System.Windows.Forms (in system.windows.forms.dll)
'Declaration Public Event CellParsing As DataGridViewCellParsingEventHandler 'Usage Dim instance As DataGridView Dim handler As DataGridViewCellParsingEventHandler AddHandler instance.CellParsing, handler
/** @event */ public void add_CellParsing (DataGridViewCellParsingEventHandler value) /** @event */ public void remove_CellParsing (DataGridViewCellParsingEventHandler value)
JScript supports the use of events, but not the declaration of new ones.
By default, the DataGridView control will attempt to convert a user-specified value displayed in a cell to an actual underlying cell value in the type specified by the cell ValueType property. This conversion uses the formatting properties of the cell style returned by the cell InheritedStyle property.
If the standard conversion does not meet your needs, handle the CellParsing event to provide custom value conversion to the required type.
Users can enter edit mode using the method specified by the EditMode property, and can leave edit mode, committing any changes to a cell, by moving to another cell or by pressing ENTER. Pressing ESC will revert any changes to the value before it is committed, and the CellParsing event will not occur. The CellParsing event occurs only if the cell value has actually been modified, even if the final value is the same as the original value. It also occurs when the CommitEdit method is called.
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 ConvertEventArgs.Value is a null reference (Nothing in Visual Basic) or is not of the correct type, or the DataGridViewCellParsingEventArgs.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.
The following code example shows how to handle the CellParsing event. It also shows how to use the DataGridViewCellParsingEventArgs class.
' Handling CellParsing allows one to accept user input, then map it to a different ' internal representation. Private Sub dataGridView1_CellParsing(ByVal sender As Object, _ ByVal e As DataGridViewCellParsingEventArgs) _ Handles dataGridView1.CellParsing If Me.dataGridView1.Columns(e.ColumnIndex).Name = _ "Release Date" Then If e IsNot Nothing Then If e.Value IsNot Nothing Then 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 ex As FormatException ' Set to false in case another CellParsing handler ' wants to try to parse this DataGridViewCellParsingEventArgs instance. e.ParsingApplied = False End Try End If End If End If End Sub
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
DataGridView ClassDataGridView Members
System.Windows.Forms Namespace
DataGridView.CellParsing Event
OnCellParsing
CommitEdit
DataGridView.CellFormatting Event
DataGridView.EditMode Property
DataGridViewCell.ValueType
DataGridViewCell.InheritedStyle
DataGridViewCell.ParseFormattedValue
DataGridViewCellParsingEventHandler
DataGridViewCellParsingEventArgs
DataGridViewCellParsingEventArgs.InheritedCellStyle
DataGridViewCellParsingEventArgs.ParsingApplied
ConvertEventArgs.Value Property
DataGridViewCellStyle
DataGridViewCellStyle.NullValue
DataGridViewCellStyle.Format
DataGridViewCellStyle.FormatProvider
Other Resources
Cell Styles in the Windows Forms DataGridView ControlDataGridView Control (Windows Forms)