Export (0) Print
Expand All

DataGridViewColumn.ContextMenuStrip Property

Gets or sets the shortcut menu for the column.

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

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

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

public override function get ContextMenuStrip () : ContextMenuStrip

public override function set ContextMenuStrip (value : ContextMenuStrip)

Not applicable.

Property Value

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

The shortcut menu appears when a user clicks the right mouse button in the column's display area.

The following code example uses the ContextMenuStrip property to add functionality for changing a cell's background color.

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft