DataGridViewRow.ContextMenuStrip Property

Definition

Gets or sets the shortcut menu for the row.

C#
public override System.Windows.Forms.ContextMenuStrip ContextMenuStrip { get; set; }
C#
public override System.Windows.Forms.ContextMenuStrip? ContextMenuStrip { get; set; }

Property Value

The ContextMenuStrip associated with the current DataGridViewRow. The default is null.

Exceptions

When getting the value of this property, the row is in a DataGridView control and is a shared row.

Examples

The following code example demonstrates how to use the DataGridViewColumn.ContextMenuStrip property, which is nearly identical to the ContextMenuStrip property of the DataGridViewRow class. This code example is part of a larger code example provided in the DataGridViewColumn class.

C#
ToolStripMenuItem toolStripItem1 = new ToolStripMenuItem();

private void AddContextMenu()
{
    toolStripItem1.Text = "Redden";
    toolStripItem1.Click += new EventHandler(toolStripItem1_Click);
    ContextMenuStrip strip = new ContextMenuStrip();
    foreach (DataGridViewColumn column in dataGridView.Columns)
    {

        column.ContextMenuStrip = strip;
        column.ContextMenuStrip.Items.Add(toolStripItem1);
    }
}

private DataGridViewCellEventArgs mouseLocation;

// Change the cell's color.
private void toolStripItem1_Click(object sender, EventArgs args)
{
    dataGridView.Rows[mouseLocation.RowIndex]
        .Cells[mouseLocation.ColumnIndex].Style.BackColor
        = Color.Red;
}

// Deal with hovering over a cell.
private void dataGridView_CellMouseEnter(object sender,
    DataGridViewCellEventArgs location)
{
    mouseLocation = location;
}

Remarks

This property indicates the shortcut menu that is displayed when the user right-clicks the row unless the DataGridView control CellContextMenuStripNeeded event or the row ContextMenuStrip property overrides the shortcut menu for the specific cell that was clicked.

When the DataGridView control DataSource property is set or its VirtualMode property is true, getting the value of the ContextMenuStrip property raises the RowContextMenuStripNeeded event of the control and returns the value of the DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip property as specified in the event handler. If there are no handlers for the event, getting the value of the ContextMenuStrip property returns the previously specified value or its default value of null.

Handling the RowContextMenuStripNeeded event is primarily useful when working with large amounts of data to avoid performance penalties when setting the row ContextMenuStrip value for multiple rows. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Changing this property raises the RowContextMenuStripChanged event on the owning DataGridView, if one exists.

If the current row is shared, use the GetContextMenuStrip method rather than getting the value of this property.

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