DataGridViewBand.ContextMenuStrip Property

Definition

Gets or sets the shortcut menu for the band.

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

Property Value

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

Examples

The following code example uses the ContextMenuStrip property of a DataGridViewColumn to add functionality for changing a cell's background color. This code example is part of a larger example provided for 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

The shortcut menu appears when a user clicks the right mouse button in the band's display area. The display area is a DataGridViewColumn or DataGridViewRow in a DataGridView.

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