Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

DataGridView.CellErrorTextNeeded-Ereignis

 

Veröffentlicht: Oktober 2016

Tritt auf, wenn der Fehlertext einer Zelle benötigt wird.

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

public event DataGridViewCellErrorTextNeededEventHandler CellErrorTextNeeded

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

When you handle the E:System.Windows.Forms.DataGridView.CellErrorTextNeeded event and specify error text in the handler, an error glyph appears in the cell unless the P:System.Windows.Forms.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 E:System.Windows.Forms.DataGridView.CellErrorTextNeeded event also occurs whenever the value of the P:System.Windows.Forms.DataGridViewCell.ErrorText property is retrieved.

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

Handle the E:System.Windows.Forms.DataGridView.CellErrorTextNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell P:System.Windows.Forms.DataGridViewCell.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 NIB: Consuming Events.

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.

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

.NET Framework
Verfügbar seit 2.0
Zurück zum Anfang
Anzeigen: