DataGridViewCell.ErrorText Property


Gets or sets the text describing an error condition associated with the cell.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Property ErrorText As String

Property Value

Type: System.String

The text that describes an error condition associated with the cell.

Typically, the ErrorText property is used when handling the CellValidating event of the DataGridView. 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. The text you specify is then displayed by the DataGridView, and the user is prompted to fix the error in the cell's data.

When the VirtualMode property of the DataGridView is true, you can provide error text for rows and cells using the RowErrorTextNeeded and CellErrorTextNeeded events.

When you assign a different ErrorText string to a cell, the CellErrorTextChanged event of the DataGridView control is raised.

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:

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

The following code example demonstrates how to use this property when handling error conditions in an unbound DataGridView. The AnnotateCell method sets an error message string to the ErrorText property.

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

    Dim column As DataGridViewColumn = _

    If column.Name = "Track" Then
    ElseIf column.Name = "Release Date" Then
    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)
    newValue.Cancel = True
End Sub

Private Sub CheckDate(ByVal newValue As DataGridViewCellValidatingEventArgs)
        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( _
    cell.ErrorText = errorMessage
End Sub

.NET Framework
Available since 2.0
Return to top