DataGridViewCell.ErrorText プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataGridViewCell.ErrorText プロパティの意味・解説 

DataGridViewCell.ErrorText プロパティ

メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。

セルに関連付けられたエラー条件を記述するテキストを取得または設定します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Dim instance As DataGridViewCell
Dim value As String

value = instance.ErrorText

instance.ErrorText = value
public string ErrorText { get;
 set; }
/** @property */
public String get_ErrorText ()

/** @property */
public void set_ErrorText (String value)

プロパティ値
セルに関連付けられたエラー条件を記述するテキスト。

解説解説

通常、ErrorText プロパティは、DataGridView の CellValidating イベントを処理する際に使用されます。セル値が特定の妥当性検査の基準を満たしていない場合、ErrorText プロパティを設定し、DataGridViewCellValidatingEventArgs の Cancel プロパティを true に設定することでコミット操作をキャンセルします。その後、指定したテキストが DataGridView に表示され、ユーザーにセルのデータのエラー修正を求めるダイアログ ボックスが表示されます。

DataGridView の VirtualMode プロパティが true の場合、RowErrorTextNeeded イベントおよび CellErrorTextNeeded イベントを使用して、行とセルのエラー テキストを提供できます。

別の ErrorText 文字列をセルに割り当てると、DataGridView コントロールの CellErrorTextChanged イベントが発生します。

使用例使用例

非バインドの DataGridView でエラー条件を処理する際にこのプロパティを使用する方法を次のコード例に示します。AnnotateCell メソッドが、エラー メッセージ文字列を ErrorText プロパティに設定します。

Private Sub dataGridView1_CellValidating(ByVal
 sender As Object, _
    ByVal e As _
    DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Dim column As DataGridViewColumn = _
        dataGridView1.Columns(e.ColumnIndex)

    If column.Name = "Track" Then
        CheckTrack(e)
    ElseIf column.Name = "Release Date"
 Then
        CheckDate(e)
    End If
End Sub

Private Shared Sub CheckTrack(ByVal
 newValue As DataGridViewCellValidatingEventArgs)
    If String.IsNullOrEmpty(newValue.FormattedValue.ToString())
 Then
        NotifyUserAndForceRedo("Please enter a track",
 newValue)
    ElseIf Not Integer.TryParse(
 _
        newValue.FormattedValue.ToString(), New Integer())
 Then
        NotifyUserAndForceRedo("A Track must be a number",
 newValue)
    ElseIf Integer.Parse(newValue.FormattedValue.ToString())
 < 1 Then
        NotifyUserAndForceRedo("Not a valid track",
 newValue)
    End If
End Sub

Private Shared Sub NotifyUserAndForceRedo(ByVal
 errorMessage As String, ByVal
 newValue As DataGridViewCellValidatingEventArgs)
    MessageBox.Show(errorMessage)
    newValue.Cancel = True
End Sub

Private Sub CheckDate(ByVal
 newValue As DataGridViewCellValidatingEventArgs)
    Try
        DateTime.Parse(newValue.FormattedValue.ToString()).ToLongDateString()
        AnnotateCell(String.Empty, newValue)
    Catch ex As FormatException
        AnnotateCell("You did not enter a valid date.",
 newValue)
    End Try
End Sub

Private Sub AnnotateCell(ByVal
 errorMessage As String, _
    ByVal editEvent As DataGridViewCellValidatingEventArgs)

    Dim cell As DataGridViewCell = _
        dataGridView1.Rows(editEvent.RowIndex).Cells( _
            editEvent.ColumnIndex)
    cell.ErrorText = errorMessage
End Sub
private void dataGridView1_CellValidating(object
 sender, DataGridViewCellValidatingEventArgs e)
{

    DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];

    if (column.Name == "Track")
    {
        CheckTrack(e);
    }
    else if (column.Name == "Release Date")
    {
        CheckDate(e);
    }
}

private static void CheckTrack(DataGridViewCellValidatingEventArgs
 newValue)
{
    Int32 ignored = new Int32();
    if (String.IsNullOrEmpty(newValue.FormattedValue.ToString()))
    {
        NotifyUserAndForceRedo("Please enter a track", newValue);
    }
    else if (!Int32.TryParse(newValue.FormattedValue.ToString(),
 out ignored))
    {
        NotifyUserAndForceRedo("A Track must be a number", newValue);
    }
    else if (Int32.Parse(newValue.FormattedValue.ToString())
 < 1)
    {
        NotifyUserAndForceRedo("Not a valid track", newValue);
    }
}

private static void NotifyUserAndForceRedo(string
 errorMessage, DataGridViewCellValidatingEventArgs newValue)
{
    MessageBox.Show(errorMessage);
    newValue.Cancel = true;
}

private void CheckDate(DataGridViewCellValidatingEventArgs
 newValue)
{
    try
    {
        DateTime.Parse(newValue.FormattedValue.ToString()).ToLongDateString();
        AnnotateCell(String.Empty, newValue);
    }
    catch (FormatException)
    {
        AnnotateCell("You did not enter a valid date.", newValue);
    }
}

private void AnnotateCell(string
 errorMessage, DataGridViewCellValidatingEventArgs editEvent)
{

    DataGridViewCell cell = dataGridView1.Rows[editEvent.RowIndex].Cells[editEvent.ColumnIndex];
    cell.ErrorText = errorMessage;
}
void dataGridView1_CellValidating( Object^ /*sender*/, DataGridViewCellValidatingEventArgs^
 newValue )
{
   DataGridViewColumn^ column = dataGridView1->Columns[ newValue->ColumnIndex
 ];
   if ( column->Name->Equals( "Track" ) )
   {
      CheckTrack( newValue );
   }
   else
   if ( column->Name->Equals( "Release Date" )
 )
   {
      CheckDate( newValue );
   }
}

void CheckTrack( DataGridViewCellValidatingEventArgs^ newValue
 )
{
   Int32 ignored;
   if ( newValue->FormattedValue->ToString() == String::Empty
 )
   {
      NotifyUserAndForceRedo( "Please enter a track", newValue );
   }
   else
   if (  !Int32::TryParse( newValue->FormattedValue->ToString(),
 ignored ) )
   {
      NotifyUserAndForceRedo( "A Track must be a number", newValue );
   }
   else
   if ( Int32::Parse( newValue->FormattedValue->ToString()
 ) < 1 )
   {
      NotifyUserAndForceRedo( "Not a valid track", newValue );
      editedLastColumn = true;
   }
}

void NotifyUserAndForceRedo( String^ errorMessage, DataGridViewCellValidatingEventArgs^
 newValue )
{
   MessageBox::Show( errorMessage );
   newValue->Cancel = true;
}

void CheckDate( DataGridViewCellValidatingEventArgs^ newValue
 )
{
   try
   {
      DateTime::Parse( newValue->FormattedValue->ToString() ).ToLongDateString();
      AnnotateCell( String::Empty, newValue );
   }
   catch ( FormatException^ /*ex*/ ) 
   {
      AnnotateCell( "You did not enter a valid date.", newValue );
   }
}

void AnnotateCell( String^ errorMessage, DataGridViewCellValidatingEventArgs^
 editEvent )
{
   DataGridViewCell^ cell = dataGridView1->Rows[ editEvent->RowIndex ]->Cells[
 editEvent->ColumnIndex ];
   cell->ErrorText = errorMessage;
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGridViewCell クラス
DataGridViewCell メンバ
System.Windows.Forms 名前空間
DataGridView クラス
DataGridViewCellValidatingEventArgs
DataGridView.CellValidating イベント
DataGridView.VirtualMode プロパティ
DataGridView.RowErrorTextNeeded イベント
DataGridView.CellErrorTextNeeded イベント
DataGridView.CellErrorTextChanged イベント
CancelEventArgs.Cancel


このページでは「.NET Framework クラス ライブラリ リファレンス」からDataGridViewCell.ErrorText プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDataGridViewCell.ErrorText プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDataGridViewCell.ErrorText プロパティ を検索

英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「DataGridViewCell.ErrorText プロパティ」の関連用語

DataGridViewCell.ErrorText プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataGridViewCell.ErrorText プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS