DataGridView.RowContextMenuStripNeeded イベント

定義

行のショートカット メニューが必要な場合に発生します。

public:
 event System::Windows::Forms::DataGridViewRowContextMenuStripNeededEventHandler ^ RowContextMenuStripNeeded;
public event System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventHandler RowContextMenuStripNeeded;
public event System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventHandler? RowContextMenuStripNeeded;
member this.RowContextMenuStripNeeded : System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventHandler 
Public Custom Event RowContextMenuStripNeeded As DataGridViewRowContextMenuStripNeededEventHandler 

イベントの種類

次のコード例では、 イベントを RowContextMenuStripNeeded 処理して、従業員のタイトルに基づいて を提供 ContextMenuStrip します。 この例では、マネージャー用と他のすべての従業員用の 2 つのショートカット メニューがあります。

void dataGridView1_RowContextMenuStripNeeded(object sender,
    DataGridViewRowContextMenuStripNeededEventArgs e)
{
    DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];

    toolStripMenuItem1.Enabled = true;

    // Show the appropriate ContextMenuStrip based on the employees title.
    if ((dataGridViewRow1.Cells["Title"].Value.ToString() ==
        "Sales Manager") ||
        (dataGridViewRow1.Cells["Title"].Value.ToString() ==
        "Vice President, Sales"))
    {
        e.ContextMenuStrip = managerMenuStrip;
    }
    else
    {
        e.ContextMenuStrip = employeeMenuStrip;
    }

    contextMenuRowIndex = e.RowIndex;
}
Public Sub dataGridView1_RowContextMenuStripNeeded( _
    ByVal sender As Object, _
    ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _
    Handles dataGridView1.RowContextMenuStripNeeded

    Dim dataGridViewRow1 As DataGridViewRow = _
    dataGridView1.Rows(e.RowIndex)

    toolStripMenuItem1.Enabled = True

    ' Show the appropriate ContextMenuStrip based on the employees title.
    If dataGridViewRow1.Cells("Title").Value.ToString() = _
        "Sales Manager" OrElse _
        dataGridViewRow1.Cells("Title").Value.ToString() = _
        "Vice President, Sales" Then

        e.ContextMenuStrip = managerMenuStrip
    Else
        e.ContextMenuStrip = employeeMenuStrip
    End If

    contextMenuRowIndex = e.RowIndex
End Sub

注釈

イベントはRowContextMenuStripNeeded、コントロール DataSource プロパティがDataGridView設定されているか、そのVirtualModeプロパティが のtrue場合にのみ発生します。 イベントの処理は、行の RowContextMenuStripNeeded 現在の状態または含まれている値によって決まるショートカット メニューを表示する場合に便利です。

イベントを RowContextMenuStripNeeded 処理すると、クリックされた特定のセルのショートカット メニューが オーバーライドされない限り CellContextMenuStripNeeded 、ユーザーが行を右クリックするたびに、ハンドラーで指定したショートカット メニューが表示されます。

イベントは RowContextMenuStripNeeded 、プログラムによって、またはユーザーが行を DataGridViewRow.ContextMenuStrip 右クリックしたときに、プロパティの値が取得されるたびにも発生します。

プロパティを DataGridViewRowContextMenuStripNeededEventArgs.RowIndex 使用して、行の状態または含まれる値を確認し、この情報を使用してプロパティを DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip 変更または変更できます。 このプロパティは、イベント値がオーバーライドする row ContextMenuStrip プロパティの値で初期化されます。

大量のデータを RowContextMenuStripNeeded 操作するときに イベントを処理して、複数の行の行 ContextMenuStrip 値を設定するパフォーマンスの低下を回避します。 詳細については、「 Windows フォーム DataGridView コントロールを拡張するための推奨される手順」を参照してください。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

適用対象

こちらもご覧ください