Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

DataGridViewCellErrorTextNeededEventHandler Delegate

Note: This delegate is new in the .NET Framework version 2.0.

Represents the method that will handle the CellErrorTextNeeded event of a DataGridView.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public delegate void DataGridViewCellErrorTextNeededEventHandler (
	Object sender,
	DataGridViewCellErrorTextNeededEventArgs e
)
/** @delegate */
public delegate void DataGridViewCellErrorTextNeededEventHandler (
	Object sender, 
	DataGridViewCellErrorTextNeededEventArgs e
)
JScript supports the use of delegates, but not the declaration of new ones.

Parameters

sender

The source of the event.

e

A DataGridViewCellErrorTextNeededEventArgs that contains the event data.

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 DataGridView.ShowCellErrors property is set to false or the cell is in edit mode. When the user moves the mouse pointer over the error glyph, 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 handling events, see Consuming Events.

When you create a DataGridViewCellErrorTextNeededEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

The following code example demonstrates a DataGridViewCellErrorTextNeededEventHandler delegate that sets the error text for the cell that needs it.

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

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.