DataGridViewRow.ContextMenuStrip 속성

정의

행의 바로 가기 메뉴를 가져오거나 설정합니다.

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

속성 값

현재 ContextMenuStrip와 연결된 DataGridViewRow입니다. 기본값은 null입니다.

예외

이 속성의 값을 가져올 때 행이 DataGridView 컨트롤 안에 있고 공유 행인 경우

예제

다음 코드 예제를 사용 DataGridViewColumn.ContextMenuStrip 하는 방법에 설명 합니다 속성은 클래스의 DataGridViewRow 속성과 거의 동일 ContextMenuStrip 합니다. 이 코드 예제는 클래스에 제공된 더 큰 코드 예제의 DataGridViewColumn 일부입니다.

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

설명

이 속성은 컨트롤 CellContextMenuStripNeeded 이벤트 또는 행 속성이 클릭한 특정 셀의 바로 가기 메뉴를 재정의하지 않는 한 DataGridView 사용자가 행 ContextMenuStrip 을 마우스 오른쪽 단추로 클릭할 때 표시되는 바로 가기 메뉴를 나타냅니다.

DataGridView 컨트롤 DataSource 속성이 설정되거나 해당 VirtualMode 속성이 true이면 속성 값을 ContextMenuStrip 가져오면 컨트롤의 이벤트가 발생 RowContextMenuStripNeeded 하며 이벤트 처리기에 지정된 대로 속성의 DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip 값이 반환됩니다. 이벤트에 대한 처리기가 없는 경우 속성 값을 ContextMenuStrip 가져오면 이전에 지정한 값 또는 의 기본값 null이 반환됩니다.

이벤트 처리는 여러 행에 RowContextMenuStripNeeded 대한 행 ContextMenuStrip 값을 설정할 때 성능 저하를 방지하기 위해 대량의 데이터로 작업할 때 주로 유용합니다. 자세한 내용은 Windows Forms DataGridView 컨트롤의 크기를 조정하는 최선의 방법을 참조하세요.

이 속성을 변경하면 소유DataGridView하는 에서 이벤트가 발생 RowContextMenuStripChanged 합니다(있는 경우).

현재 행이 공유되는 경우 이 속성의 GetContextMenuStrip 값을 가져오는 대신 메서드를 사용합니다.

적용 대상

추가 정보