DataGridView.CellErrorTextNeeded Event

Definition

Occurs when a cell's error text is needed.

C#
public event System.Windows.Forms.DataGridViewCellErrorTextNeededEventHandler CellErrorTextNeeded;
C#
public event System.Windows.Forms.DataGridViewCellErrorTextNeededEventHandler? CellErrorTextNeeded;

Event Type

Examples

The following code example demonstrates how to create error messages for cells without unsharing rows or causing the cell error to be duplicated over all the shared cells.

C#
private ToolStripMenuItem wholeTable = new ToolStripMenuItem();
private ToolStripMenuItem lookUp = new ToolStripMenuItem();
private ContextMenuStrip strip;
private string cellErrorText;

private void dataGridView1_CellContextMenuStripNeeded(object sender,
    DataGridViewCellContextMenuStripNeededEventArgs e)
{
    cellErrorText = String.Empty;

    if (strip == null)
    {
        strip = new ContextMenuStrip();
        lookUp.Text = "Look Up";
        wholeTable.Text = "See Whole Table";
        strip.Items.Add(lookUp);
        strip.Items.Add(wholeTable);
    }
    e.ContextMenuStrip = strip;
}

private void wholeTable_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = Populate("Select * from employees", true);
}

private DataGridViewCellEventArgs theCellImHoveringOver;

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    theCellImHoveringOver = e;
}

private DataGridViewCellEventArgs cellErrorLocation;

private void lookUp_Click(object sender, EventArgs e)
{
    try
    {
        dataGridView1.DataSource = Populate("Select * from employees where " +
            dataGridView1.Columns[theCellImHoveringOver.ColumnIndex].Name + " = '" +
            dataGridView1.Rows[theCellImHoveringOver.RowIndex].
            Cells[theCellImHoveringOver.ColumnIndex].Value + "'",
            true);
    }
    catch (SqlException)
    {
        cellErrorText = "Can't look this cell up";
        cellErrorLocation = theCellImHoveringOver;
    }
}

private void dataGridView1_CellErrorTextNeeded(object sender,
    DataGridViewCellErrorTextNeededEventArgs e)
{
    if (cellErrorLocation != null)
    {
        if (e.ColumnIndex == cellErrorLocation.ColumnIndex &&
            e.RowIndex == cellErrorLocation.RowIndex)
        {
            e.ErrorText = cellErrorText;
        }
    }
}

private DataTable Populate(string query, bool resetUnsharedCounter)
{
    if (resetUnsharedCounter)
    {
        ResetCounter();
    }

    // Alter the data source as necessary
    SqlDataAdapter adapter = new SqlDataAdapter(query,
        new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
        "Initial Catalog=Northwind;Data Source=localhost"));

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);
    return table;
}

private Label count = new Label();
private int unsharedRowCounter;

private void ResetCounter()
{
    unsharedRowCounter = 0;
    count.Text = unsharedRowCounter.ToString();
}

Remarks

The CellErrorTextNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true. Handling the CellErrorTextNeeded event is useful when you want to determine the error for a cell depending on its value or state.

When you handle the CellErrorTextNeeded event and specify error text in the handler, an error glyph appears in the cell unless the ShowCellErrors property is set to false or the cell is in edit mode. When the user moves the mouse pointer over the error glyph or navigates to the cell using the keyboard, the error text appears in a ToolTip.

The CellErrorTextNeeded event also occurs whenever the value of the DataGridViewCell.ErrorText property is retrieved.

You can use the DataGridViewCellEventArgs.RowIndex and ColumnIndex property to determine the state or value of a cell, and use this information to change or modify the DataGridViewCellErrorTextNeededEventArgs.ErrorText property. This property is initialized with the value of the cell ErrorText property, which the event value overrides.

Handle the CellErrorTextNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell ErrorText value for multiple cells. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

For more information about how to handle events, see Handling and Raising Events.

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