DataGridViewRowContextMenuStripNeededEventHandler デリゲート
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Public Delegate Sub DataGridViewRowContextMenuStripNeededEventHandler ( _ sender As Object, _ e As DataGridViewRowContextMenuStripNeededEventArgs _ )
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 )

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; }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


System.Windows.Forms 名前空間
ContextMenuStrip クラス
DataGridView クラス
DataGridView.RowContextMenuStripNeeded イベント
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridViewRowContextMenuStripNeededEventArgs クラス
DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip プロパティ
DataGridViewRow.ContextMenuStrip プロパティ
その他の技術情報
Windows フォーム DataGridView コントロールを拡張するための推奨される手順
- DataGridViewRowContextMenuStripNeededEventHandler デリゲートのページへのリンク