DataGridViewRow.ContextMenuStrip Eigenschap

Definitie

Hiermee haalt u het snelmenu voor de rij op of stelt u deze in.

public:
 virtual property System::Windows::Forms::ContextMenuStrip ^ ContextMenuStrip { System::Windows::Forms::ContextMenuStrip ^ get(); void set(System::Windows::Forms::ContextMenuStrip ^ value); };
public override System.Windows.Forms.ContextMenuStrip ContextMenuStrip { get; set; }
public override System.Windows.Forms.ContextMenuStrip? ContextMenuStrip { get; set; }
member this.ContextMenuStrip : System.Windows.Forms.ContextMenuStrip with get, set
Public Overrides Property ContextMenuStrip As ContextMenuStrip

Waarde van eigenschap

De ContextMenuStrip gekoppeld aan de huidige DataGridViewRow. De standaardwaarde is null.

Uitzonderingen

Wanneer u de waarde van deze eigenschap ophaalt, bevindt de rij zich in een DataGridView besturingselement en is deze een gedeelde rij.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u de DataGridViewColumn.ContextMenuStrip eigenschap gebruikt, die bijna identiek is aan de ContextMenuStrip eigenschap van de DataGridViewRow klasse. Dit codevoorbeeld maakt deel uit van een groter codevoorbeeld in de DataGridViewColumn klasse.

ToolStripMenuItem^ toolStripItem1;
void AddContextMenu()
{
   toolStripItem1->Text = L"Redden";
   toolStripItem1->Click += gcnew EventHandler( this, &DataGridViewColumnDemo::toolStripItem1_Click );
   System::Windows::Forms::ContextMenuStrip^ strip = gcnew System::Windows::Forms::ContextMenuStrip;
   IEnumerator^ myEnum = dataGridView->Columns->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DataGridViewColumn^ column = safe_cast<DataGridViewColumn^>(myEnum->Current);
      column->ContextMenuStrip = strip;
      column->ContextMenuStrip->Items->Add( toolStripItem1 );
   }
}

DataGridViewCellEventArgs^ mouseLocation;

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


// Deal with hovering over a cell.
void dataGridView_CellMouseEnter( Object^ /*sender*/, DataGridViewCellEventArgs^ location )
{
   mouseLocation = location;
}
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;
}
WithEvents toolStripItem1 As New ToolStripMenuItem()

Private Sub AddContextMenu()
    toolStripItem1.Text = "Redden"
    Dim strip As New ContextMenuStrip()
    For Each column As DataGridViewColumn _
        In dataGridView.Columns()

        column.ContextMenuStrip = strip
        column.ContextMenuStrip.Items.Add(toolStripItem1)
    Next
End Sub
' Change the cell's color.
Private Sub toolStripItem1_Click(ByVal sender As Object, _
    ByVal args As EventArgs) _
    Handles toolStripItem1.Click

    dataGridView.Rows(mouseLocation.RowIndex) _
        .Cells(mouseLocation.ColumnIndex) _
        .Style.BackColor = Color.Red
End Sub

Private mouseLocation As DataGridViewCellEventArgs

' Deal with hovering over a cell.
Private Sub dataGridView_CellMouseEnter(ByVal sender As Object, _
    ByVal location As DataGridViewCellEventArgs) _
    Handles DataGridView.CellMouseEnter

    mouseLocation = location
End Sub

Opmerkingen

Deze eigenschap geeft het snelmenu aan dat wordt weergegeven wanneer de gebruiker met de rechtermuisknop op de rij klikt, tenzij de DataGridView besturingsgebeurtenis CellContextMenuStripNeeded of de rijeigenschap ContextMenuStrip het snelmenu overschrijft voor de specifieke cel waarop is geklikt.

Wanneer de eigenschap van het DataGridView besturingselement DataSource is ingesteld of VirtualMode de eigenschap is true, wordt de waarde van de ContextMenuStrip eigenschap opgehaald door de RowContextMenuStripNeeded gebeurtenis van het besturingselement op te halen en wordt de waarde van de DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip eigenschap geretourneerd zoals opgegeven in de gebeurtenis-handler. Als er geen handlers voor de gebeurtenis zijn, retourneert het ophalen van de waarde van de ContextMenuStrip eigenschap de eerder opgegeven waarde of de standaardwaarde van null.

Het afhandelen van de RowContextMenuStripNeeded gebeurtenis is voornamelijk handig bij het werken met grote hoeveelheden gegevens om prestatiestraffen te voorkomen bij het instellen van de rijwaarde ContextMenuStrip voor meerdere rijen. Zie Best practices voor het schalen van het Windows Forms DataGridView-besturingselement voor meer informatie.

Als u deze eigenschap wijzigt, wordt de RowContextMenuStripChanged gebeurtenis voor de eigenaar DataGridViewgegenereerd, indien aanwezig.

Als de huidige rij wordt gedeeld, gebruikt u de GetContextMenuStrip methode in plaats van de waarde van deze eigenschap op te halen.

Van toepassing op

Zie ook