次の方法で共有


DataGridViewCellParsingEventArgs クラス

定義

CellParsing コントロールのDataGridView イベントのデータを提供します。

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
継承
DataGridViewCellParsingEventArgs

次のコード例は、 DataGridViewCellParsingEventArgs を使用して日付エントリの有効性を確認する方法を示しています。

// 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

注釈

CellParsing イベントを処理して、ユーザー指定の値からセル ValueType プロパティで指定された型の値へのカスタム値変換を提供します。

CellParsing イベントを処理する場合は、値を自分で変換するか、既定の変換をカスタマイズできます。 たとえば、セル ParseFormattedValue メソッドと任意の型コンバーターを使用して、自分で値を変換できます。 または、既定の型コンバーターで値を解析できますが、セル InheritedStyle プロパティを使用して初期化される、DataGridViewCellParsingEventArgs.InheritedCellStyle プロパティによって返されるオブジェクトのNullValueDataSourceNullValue、およびFormatProviderプロパティを変更することもできます。

値を自分で変換するときは、 ConvertEventArgs.Value プロパティの最初の書式設定された値を、セル ValueType プロパティで指定された型の変換後の値に置き換えます。 これ以上解析する必要がないことを示すには、 DataGridViewCellParsingEventArgs.ParsingApplied プロパティを true に設定します。

イベント ハンドラーが完了すると、 Valuenull または正しい型ではない場合、または ParsingApplied プロパティが falseされている場合、 Value は、既定の型コンバーターを使用してセル ParseFormattedValue メソッドを使用して解析されます。 このメソッドの既定の実装では、渡されたセル スタイルの NullValueDataSourceNullValue、および FormatProvider プロパティを使用して値を解析します。 値が NullValueと等しくない場合は、 FormatProvider プロパティと渡された型コンバーターを使用して値が解析されます。

セル値を表示用の書式設定された値に変換するようにカスタマイズするには、 CellFormatting イベントを処理します。

イベントの処理方法の詳細については、「イベントの 処理と発生」を参照してください。

コンストラクター

名前 説明
DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle)

DataGridViewCellParsingEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
ColumnIndex

解析が必要なセル データの列インデックスを取得します。

DesiredType

目的の値のデータ型を取得します。

(継承元 ConvertEventArgs)
InheritedCellStyle

編集したセルに適用されるスタイルを取得または設定します。

ParsingApplied

セルの値が正常に解析されたかどうかを示す値を取得または設定します。

RowIndex

解析が必要なセルの行インデックスを取得します。

Value

ConvertEventArgsの値を取得または設定します。

(継承元 ConvertEventArgs)

メソッド

名前 説明
Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください