DataGridViewCell.ErrorText Property

Definition

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

C#
[System.ComponentModel.Browsable(false)]
public string ErrorText { get; set; }

Property Value

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

Attributes

Examples

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.

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

Remarks

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:

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

Applies to

Product Versions
.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

See also