DataGridView.RowContextMenuStripNeeded イベントとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataGridView.RowContextMenuStripNeeded イベントの意味・解説 

DataGridView.RowContextMenuStripNeeded イベント

メモ : このイベントは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Event RowContextMenuStripNeeded As
 DataGridViewRowContextMenuStripNeededEventHandler
Dim instance As DataGridView
Dim handler As DataGridViewRowContextMenuStripNeededEventHandler

AddHandler instance.RowContextMenuStripNeeded, handler
public event DataGridViewRowContextMenuStripNeededEventHandler
 RowContextMenuStripNeeded
public:
event DataGridViewRowContextMenuStripNeededEventHandler^ RowContextMenuStripNeeded
 {
    void add (DataGridViewRowContextMenuStripNeededEventHandler^
 value);
    void remove (DataGridViewRowContextMenuStripNeededEventHandler^
 value);
}
/** @event */
public void add_RowContextMenuStripNeeded (DataGridViewRowContextMenuStripNeededEventHandler
 value)

/** @event */
public void remove_RowContextMenuStripNeeded
 (DataGridViewRowContextMenuStripNeededEventHandler value)
JScript では、イベントは使用できますが、新規に宣言することはできません。
解説解説

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

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

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

DataGridViewRowContextMenuStripNeededEventArgs.RowIndex プロパティを使用すると、行の状態や行に含まれる値を特定し、その情報を使用して DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip プロパティを変更できます。このプロパティは、行の ContextMenuStrip プロパティの値によって初期化され、イベントの値によってオーバーライドされます。

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

イベント処理の詳細については、「イベントの利用」を参照してください。

使用例使用例

RowContextMenuStripNeeded イベントを処理して、従業員の役職に応じた ContextMenuStrip を表示するコード例を次に示します。この例では、2 種類のショートカット メニューがあり、1 つは経営者向け、もう 1 つはその他のすべての従業員向けです。

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
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;
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGridView クラス
DataGridView メンバ
System.Windows.Forms 名前空間
ContextMenuStrip クラス
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridViewRowContextMenuStripNeededEventHandler
DataGridViewRowContextMenuStripNeededEventArgs
DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewRow.ContextMenuStrip
OnRowContextMenuStripNeeded
その他の技術情報
DataGridView コントロール (Windows フォーム)



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「DataGridView.RowContextMenuStripNeeded イベント」の関連用語

DataGridView.RowContextMenuStripNeeded イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataGridView.RowContextMenuStripNeeded イベントのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS