DataGridViewCell.ErrorText 속성

정의

셀과 연결된 오류 조건을 설명하는 텍스트를 가져오거나 설정합니다.Gets or sets the text describing an error condition associated with the cell.

public:
 property System::String ^ ErrorText { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public string ErrorText { get; set; }
member this.ErrorText : string with get, set
Public Property ErrorText As String

속성 값

셀과 연결된 오류 조건을 설명하는 텍스트입니다.The text that describes an error condition associated with the cell.

특성

예제

다음 코드 예제에서는 바인딩되지 않은 DataGridView에서 오류 조건을 처리할 때이 속성을 사용 하는 방법을 보여 줍니다.The following code example demonstrates how to use this property when handling error conditions in an unbound DataGridView. AnnotateCell 메서드는 오류 메시지 문자열을 ErrorText 속성으로 설정 합니다.The AnnotateCell method sets an error message string to the ErrorText property.

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

설명

일반적으로 ErrorText 속성은 DataGridViewCellValidating 이벤트를 처리할 때 사용 됩니다.Typically, the ErrorText property is used when handling the CellValidating event of the DataGridView. 셀의 값이 일부 유효성 검사 조건에 실패 하는 경우 ErrorText 속성을 설정 하 고 DataGridViewCellValidatingEventArgsCancel 속성을 true로 설정 하 여 커밋 작업을 취소 합니다.If the cell's value fails some validation criteria, set the ErrorText property and cancel the commit operation by setting the Cancel property of the DataGridViewCellValidatingEventArgs to true. 그러면 지정 된 텍스트가 DataGridView표시 되 고 사용자에 게 셀 데이터의 오류를 수정 하 라는 메시지가 표시 됩니다.The text you specify is then displayed by the DataGridView, and the user is prompted to fix the error in the cell's data.

DataGridViewVirtualMode 속성을 true하는 경우 RowErrorTextNeededCellErrorTextNeeded 이벤트를 사용 하 여 행과 셀에 대 한 오류 텍스트를 제공할 수 있습니다.When the VirtualMode property of the DataGridView is true, you can provide error text for rows and cells using the RowErrorTextNeeded and CellErrorTextNeeded events.

셀에 다른 ErrorText 문자열을 할당 하면 DataGridView 컨트롤의 CellErrorTextChanged 이벤트가 발생 합니다.When you assign a different ErrorText string to a cell, the CellErrorTextChanged event of the DataGridView control is raised.

App.config 파일에 다음 항목이 포함 되어 있으면 시스템 DPI 설정에 의해.NET Framework 4.5.2부터을 결정 됩니다 오류 아이콘의 크기를 조정 합니다.Starting with the .NET Framework 4.5.2, resizing of the error icon is determined by the system DPI setting when the app.config file contains the following entry:

<appSettings>  
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />  
</appSettings>  

적용 대상

추가 정보