DataGridViewCell.ErrorText プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGridViewCell Dim value As String value = instance.ErrorText instance.ErrorText = value
/** @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; }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGridViewCell クラス
DataGridViewCell メンバ
System.Windows.Forms 名前空間
DataGridView クラス
DataGridViewCellValidatingEventArgs
DataGridView.CellValidating イベント
DataGridView.VirtualMode プロパティ
DataGridView.RowErrorTextNeeded イベント
DataGridView.CellErrorTextNeeded イベント
DataGridView.CellErrorTextChanged イベント
CancelEventArgs.Cancel
- DataGridViewCell.ErrorText プロパティのページへのリンク