DataGridView.CellContextMenuStripNeeded Event

Definition

Occurs when a cell's shortcut menu is needed.

C#
public event System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventHandler CellContextMenuStripNeeded;
C#
public event System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventHandler? CellContextMenuStripNeeded;

Event Type

Examples

The following code example demonstrates how to use a CellContextMenuStripNeeded event handler to display a custom shortcut menu used when the user right-clicks a cell.

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 CellContextMenuStripNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true.

When you handle the CellContextMenuStripNeeded event, the shortcut menu that you specify in the handler is shown whenever the user right-clicks a cell. This is useful when you want to display a shortcut menu determined by the current state or value of a cell.

The CellContextMenuStripNeeded event also occurs whenever the value of the DataGridViewCell.ContextMenuStrip property is retrieved, either programmatically or when the user right-clicks a cell.

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

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

You can also specify shortcut menus for individual rows rather than individual cells by setting the row ContextMenuStrip property or handling the RowContextMenuStripNeeded event. The cell ContextMenuStrip property setting overrides the row ContextMenuStrip property setting, and the CellContextMenuStripNeeded event overrides both the RowContextMenuStripNeeded event and the row ContextMenuStrip property setting. However, you can specify null for a cell shortcut menu to prevent a row shortcut menu from being overridden.

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

See also