DataGridView.RowContextMenuStripNeeded イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGridView Dim handler As DataGridViewRowContextMenuStripNeededEventHandler AddHandler instance.RowContextMenuStripNeeded, handler
public: event DataGridViewRowContextMenuStripNeededEventHandler^ RowContextMenuStripNeeded { void add (DataGridViewRowContextMenuStripNeededEventHandler^ value); void remove (DataGridViewRowContextMenuStripNeededEventHandler^ value); }

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

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


DataGridView クラス
DataGridView メンバ
System.Windows.Forms 名前空間
ContextMenuStrip クラス
DataGridView.DataSource プロパティ
DataGridView.VirtualMode プロパティ
DataGridViewRowContextMenuStripNeededEventHandler
DataGridViewRowContextMenuStripNeededEventArgs
DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewRow.ContextMenuStrip
OnRowContextMenuStripNeeded
その他の技術情報
DataGridView コントロール (Windows フォーム)
- DataGridView.RowContextMenuStripNeeded イベントのページへのリンク