This documentation is archived and is not being maintained.

DataGridViewBand.ContextMenuStrip Property

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

Gets or sets the shortcut menu for the band.

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

public virtual ContextMenuStrip ContextMenuStrip { get; set; }
/** @property */
public ContextMenuStrip get_ContextMenuStrip ()

/** @property */
public void set_ContextMenuStrip (ContextMenuStrip value)

public function get ContextMenuStrip () : ContextMenuStrip

public function set ContextMenuStrip (value : ContextMenuStrip)

Property Value

The ContextMenuStrip associated with the current DataGridViewBand. The default is a null reference (Nothing in Visual Basic).

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.

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.

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

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
Show: