Redigera

Dela via


DataGridViewCellParsingEventArgs Class

Definition

Provides data for the CellParsing event of a DataGridView control.

public ref class DataGridViewCellParsingEventArgs : System::Windows::Forms::ConvertEventArgs
public class DataGridViewCellParsingEventArgs : System.Windows.Forms.ConvertEventArgs
type DataGridViewCellParsingEventArgs = class
    inherit ConvertEventArgs
Public Class DataGridViewCellParsingEventArgs
Inherits ConvertEventArgs
Inheritance
DataGridViewCellParsingEventArgs

Examples

The following code example demonstrates using DataGridViewCellParsingEventArgs 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;
            }
         }
      }
   }
}
// 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;
                }
            }
        }
    }
}
' 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

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. Alternately, 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.

Constructors

DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle)

Initializes a new instance of the DataGridViewCellParsingEventArgs class.

Properties

ColumnIndex

Gets the column index of the cell data that requires parsing.

DesiredType

Gets the data type of the desired value.

(Inherited from ConvertEventArgs)
InheritedCellStyle

Gets or sets the style applied to the edited cell.

ParsingApplied

Gets or sets a value indicating whether a cell's value has been successfully parsed.

RowIndex

Gets the row index of the cell that requires parsing.

Value

Gets or sets the value of the ConvertEventArgs.

(Inherited from ConvertEventArgs)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also