DataGridViewRow.ContextMenuStrip プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGridViewRow Dim value As ContextMenuStrip value = instance.ContextMenuStrip instance.ContextMenuStrip = value
public: virtual property ContextMenuStrip^ ContextMenuStrip { ContextMenuStrip^ get () override; void set (ContextMenuStrip^ value) override; }
/** @property */ public ContextMenuStrip get_ContextMenuStrip () /** @property */ public void set_ContextMenuStrip (ContextMenuStrip value)
public override function get ContextMenuStrip () : ContextMenuStrip public override function set ContextMenuStrip (value : ContextMenuStrip)
現在の DataGridViewRow に関連付けられている ContextMenuStrip。既定値は null 参照 (Visual Basic では Nothing) です。


DataGridView コントロールの CellContextMenuStripNeeded イベントか行の ContextMenuStrip プロパティが、クリックされた特定のセルのショートカット メニューをオーバーライドしていなければ、このプロパティは、ユーザーが行を右クリックしたときに表示されるショートカット メニューを示します。
DataGridView コントロールの DataSource プロパティが設定されている場合、または VirtualMode プロパティが true の場合、ContextMenuStrip プロパティの値を取得すると、コントロールの RowContextMenuStripNeeded イベントが発生し、イベント ハンドラに指定された DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip プロパティの値が返されます。イベントのハンドラがない場合、ContextMenuStrip プロパティの値を取得すると、以前に指定された値、またはその既定値である null 参照 (Visual Basic では Nothing) が返されます。
RowContextMenuStripNeeded イベントの処理は、大量のデータを扱う場合に、複数行の ContextMenuStrip の値を設定する際のパフォーマンスの低下を防ぐために主に使用します。詳細については、「Windows フォーム DataGridView コントロールを拡張するための推奨される手順」を参照してください。
このプロパティを変更すると、(もしあれば) 所有している側の DataGridView で RowContextMenuStripChanged イベントが発生します。
現在の行が共有されている場合は、このプロパティの値を取得するのではなく、GetContextMenuStrip メソッドを使用してください。

DataGridViewColumn.ContextMenuStrip プロパティを使用する方法を次のコード例に示します。このプロパティは、DataGridViewRow クラスの ContextMenuStrip プロパティとほぼ同じです。このコード例は、DataGridViewColumn クラスのトピックで取り上げている、コード例の一部分です。
WithEvents toolStripItem1 As New ToolStripMenuItem() Private Sub AddContextMenu() toolStripItem1.Text = "Redden" Dim strip As New ContextMenuStrip() For Each column As DataGridViewColumn _ In dataGridView.Columns() column.ContextMenuStrip = strip column.ContextMenuStrip.Items.Add(toolStripItem1) Next End Sub ' Change the cell's color. Private Sub toolStripItem1_Click(ByVal sender As Object, _ ByVal args As EventArgs) _ Handles toolStripItem1.Click dataGridView.Rows(mouseLocation.RowIndex) _ .Cells(mouseLocation.ColumnIndex) _ .Style.BackColor = Color.Red End Sub Private mouseLocation As DataGridViewCellEventArgs ' Deal with hovering over a cell. Private Sub dataGridView_CellMouseEnter(ByVal sender As Object, _ ByVal location As DataGridViewCellEventArgs) _ Handles DataGridView.CellMouseEnter mouseLocation = location End Sub
ToolStripMenuItem toolStripItem1 = new ToolStripMenuItem(); private void AddContextMenu() { toolStripItem1.Text = "Redden"; toolStripItem1.Click += new EventHandler(toolStripItem1_Click); ContextMenuStrip strip = new ContextMenuStrip(); foreach (DataGridViewColumn column in dataGridView.Columns) { column.ContextMenuStrip = strip; column.ContextMenuStrip.Items.Add(toolStripItem1); } } private DataGridViewCellEventArgs mouseLocation; // Change the cell's color. private void toolStripItem1_Click(object sender, EventArgs args) { dataGridView.Rows[mouseLocation.RowIndex] .Cells[mouseLocation.ColumnIndex].Style.BackColor = Color.Red; } // Deal with hovering over a cell. private void dataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs location) { mouseLocation = location; }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


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