download source code and demo(C# 2005) -- 25.3KB
Introduction
DataGridView's RowContextMenuStripNeeded event is useful to display a shortcut menu determined by a row's current state or the values it contains, its basic usage steps are:
- Design a ContextMenuStrip menu control, then assign it to DataGridView.RowTemplate.ContextMenuStrip.
- Capture DataGridView's RowContextMenuStripNeeded event,obtain the RowIndex and ContextMenuStrip object, and then realize self-defined event handler.
However, RowContextMenuStripNeeded events occurs only when the DataGridView DataSource is set or its VirtualMode is true.(reference MSDN: DataGridView.RowContextMenuStripNeeded Event) The RowContextMenuStripNeeded event does not occur too if DataGridView has no rows/columns or the right click outside valid rows such as ColumnHeaders/RowHeaders, empty area, etc..The another event CellContextMenuStripNeeded has the similar limitations and shortcomings.
This article introduces a solution for custuming an event for DataGridView.ContextMenuStrip property, which has the features like xxxContextMenuStripNeeded events. The main i