DataGridViewCell.ContextMenuStrip DataGridViewCell.ContextMenuStrip DataGridViewCell.ContextMenuStrip DataGridViewCell.ContextMenuStrip Property

定义

获取或设置与单元格关联的快捷菜单。Gets or sets the shortcut menu associated with the cell.

public:
 virtual property System::Windows::Forms::ContextMenuStrip ^ ContextMenuStrip { System::Windows::Forms::ContextMenuStrip ^ get(); void set(System::Windows::Forms::ContextMenuStrip ^ value); };
public virtual System.Windows.Forms.ContextMenuStrip ContextMenuStrip { get; set; }
member this.ContextMenuStrip : System.Windows.Forms.ContextMenuStrip with get, set
Public Overridable Property ContextMenuStrip As ContextMenuStrip

属性值

注解

此属性指示在用户右键单击该单元格时通常会显示的快捷菜单。This property indicates the shortcut menu that is typically displayed when the user right-clicks the cell.

DataGridView控件DataSource属性设置或其VirtualMode属性是true,获取的值ContextMenuStrip属性将引发CellContextMenuStripNeeded控件的事件,并返回的值DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip为属性指定在事件处理程序。When the DataGridView control DataSource property is set or its VirtualMode property is true, getting the value of the ContextMenuStrip property raises the CellContextMenuStripNeeded event of the control and returns the value of the DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip property as specified in the event handler. 如果没有为事件处理程序,则获取的值ContextMenuStrip属性返回以前指定的值或其默认值为nullIf there are no handlers for the event, getting the value of the ContextMenuStrip property returns the previously specified value or its default value of null.

处理CellContextMenuStripNeeded事件时,可以主要使用大量的数据以避免性能损失,设置单元格时ContextMenuStrip多个单元格的值。Handling the CellContextMenuStripNeeded event is primarily useful when working with large amounts of data to avoid performance penalties when setting the cell ContextMenuStrip value for multiple cells. 有关详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

更改此属性将引发CellContextMenuStripChanged对所具有的事件DataGridView,如果存在一个。Changing this property raises the CellContextMenuStripChanged event on the owning DataGridView, if one exists.

您还可以指定快捷菜单,为各个行而不是通过设置行的各个单元格ContextMenuStrip属性或处理DataGridView控件的RowContextMenuStripNeeded事件。You can also specify shortcut menus for individual rows rather than individual cells by setting the row ContextMenuStrip property or handling the DataGridView control's RowContextMenuStripNeeded event. 该单元格ContextMenuStrip属性设置将覆盖该行ContextMenuStrip属性设置,并CellContextMenuStripNeeded事件替代这两RowContextMenuStripNeeded事件和行ContextMenuStrip属性设置。The cell ContextMenuStrip property setting overrides the row ContextMenuStrip property setting, and the CellContextMenuStripNeeded event overrides both the RowContextMenuStripNeeded event and the row ContextMenuStrip property setting. 您可以指定null为单元格快捷菜单,但是,若要防止被覆盖的行的快捷菜单。You can specify null for a cell shortcut menu, however, to prevent a row shortcut menu from being overridden.

适用于

另请参阅