英語で読む

次の方法で共有


DataGridViewCellParsingEventHandler 代理人

定義

CellParsingDataGridView イベントを処理するメソッドを表します。

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

パラメーター

sender
Object

イベントのソース。

e
DataGridViewCellParsingEventArgs

イベント データを格納している DataGridViewCellParsingEventArgs

次のコード例では、 を使用してDataGridViewCellParsingEventHandler日付エントリの有効性をチェックします。

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;
                }
            }
        }
    }
}

注釈

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

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

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

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

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

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

DataGridViewCellParsingEventHandler デリゲートを作成する場合は、イベントを処理するメソッドを指定します。 イベント ハンドラーにイベントを関連付けるには、イベントにデリゲートのインスタンスを追加します。 イベント ハンドラーは、デリゲートを削除しない限り、イベントが発生するたびに呼び出されます。 イベント ハンドラー デリゲートの詳細については、「イベントの 処理と発生」を参照してください。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

製品 バージョン
.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, 10

こちらもご覧ください