DataGridViewCell.ContextMenuStrip 属性

定义

获取或设置与单元格关联的快捷菜单。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

属性值

ContextMenuStrip

与单元格关联的 ContextMenuStripThe ContextMenuStrip associated with the cell.

注解

此属性指示通常在用户右键单击单元格时显示的快捷菜单。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 大量数据时,处理事件主要是非常有用的,目的是为多个单元格设置单元值时避免性能下降 ContextMenuStripHandling 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 控件的事件,为单独的行而不是单个单元指定快捷菜单 RowContextMenuStripNeededYou 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 属性设置将重写 row 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.

适用于

另请参阅