DataGridViewRowContextMenuStripNeededEventHandler デリゲートとは? わかりやすく解説

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

DataGridViewRowContextMenuStripNeededEventHandler デリゲート

メモ : このデリゲートは、.NET Framework version 2.0 で新しく追加されたものです。

DataGridView の RowContextMenuStripNeeded イベントを処理するメソッドを表します。

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

Public Delegate Sub DataGridViewRowContextMenuStripNeededEventHandler
 ( _
    sender As Object, _
    e As DataGridViewRowContextMenuStripNeededEventArgs _
)
Dim instance As New DataGridViewRowContextMenuStripNeededEventHandler(AddressOf
 HandlerMethod)
public delegate void DataGridViewRowContextMenuStripNeededEventHandler
 (
    Object sender,
    DataGridViewRowContextMenuStripNeededEventArgs e
)
public delegate void DataGridViewRowContextMenuStripNeededEventHandler
 (
    Object^ sender, 
    DataGridViewRowContextMenuStripNeededEventArgs^ e
)
/** @delegate */
public delegate void DataGridViewRowContextMenuStripNeededEventHandler
 (
    Object sender, 
    DataGridViewRowContextMenuStripNeededEventArgs e
)
JScript では、デリゲートは使用できますが、新規に宣言することはできません。

パラメータ

sender

イベントのソース。

e

イベント データを格納している DataGridViewRowContextMenuStripNeededEventArgs。

解説解説

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

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

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

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

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

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

DataGridViewRowContextMenuStripNeededEventHandler デリゲートを作成する場合は、イベントを処理するメソッドを識別してください。イベントをイベント ハンドラに関連付けるには、デリゲートのインスタンスをイベントに追加します。デリゲートを削除しない限り、そのイベントが発生すると常にイベント ハンドラが呼び出されます。イベント ハンドラ デリゲートの詳細については、「イベントとデリゲート」を参照してください。

使用例使用例

次のコード例では、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;
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
System.Windows.Forms 名前空間
ContextMenuStrip クラス
DataGridView クラス
DataGridView.RowContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridViewRowContextMenuStripNeededEventArgs クラス
DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip プロパティ
DataGridViewRow.ContextMenuStrip プロパティ
その他の技術情報
Windows フォーム DataGridView コントロールを拡張するための推奨される手順



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

DataGridViewRowContextMenuStripNeededEventHandler デリゲートのお隣キーワード
検索ランキング

   

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



DataGridViewRowContextMenuStripNeededEventHandler デリゲートのページの著作権

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

©2026 GRAS Group, Inc.RSS