DataGridViewCellContextMenuStripNeededEventArgs Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
'Declaration Public Class DataGridViewCellContextMenuStripNeededEventArgs Inherits DataGridViewCellEventArgs 'Usage Dim instance As DataGridViewCellContextMenuStripNeededEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs extends DataGridViewCellEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs extends DataGridViewCellEventArgs
The CellContextMenuStripNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true.
When you handle the CellContextMenuStripNeeded event, the shortcut menu that you specify in the handler is shown whenever the user right-clicks a cell. This is useful when you want to display shortcut menus determined by the current state or value of a cell.
The CellContextMenuStripNeeded event also occurs whenever the value of the DataGridViewCell.ContextMenuStrip property is retrieved, either programmatically or when the user right-clicks the cell.
You can use the ColumnIndex and RowIndex properties to determine the state or value of a cell, and use this information to set the ContextMenuStrip property. This property is initialized with the value of the cell ContextMenuStrip property, which the event value overrides.
Handle the CellContextMenuStripNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell ContextMenuStrip value for multiple cells. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.
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. 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. You can specify a null reference (Nothing in Visual Basic) for a cell shortcut menu, however, to prevent a row shortcut menu from being overridden.
For more information about handling events, see Consuming Events.
The following code example uses the DataGridViewCellContextMenuStripNeededEventArgs class to set the shortcut menu without unsharing the row.
Private WithEvents wholeTable As New ToolStripMenuItem() Private WithEvents lookUp As New ToolStripMenuItem() Private strip As ContextMenuStrip Private cellErrorText As String Private Sub dataGridView1_CellContextMenuStripNeeded(ByVal sender As Object, _ ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _ Handles dataGridView1.CellContextMenuStripNeeded cellErrorText = String.Empty If strip Is Nothing Then strip = New ContextMenuStrip() lookUp.Text = "Look Up" wholeTable.Text = "See Whole Table" strip.Items.Add(lookUp) strip.Items.Add(wholeTable) End If e.ContextMenuStrip = strip End Sub Private Sub wholeTable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles wholeTable.Click dataGridView1.DataSource = Populate("Select * from employees", True) End Sub Private theCellImHoveringOver As DataGridViewCellEventArgs Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles dataGridView1.CellMouseEnter theCellImHoveringOver = e End Sub Private cellErrorLocation As DataGridViewCellEventArgs Private Sub lookUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lookUp.Click Try dataGridView1.DataSource = Populate("Select * from employees where " & _ dataGridView1.Columns(theCellImHoveringOver.ColumnIndex).Name & " = '" & _ dataGridView1.Rows(theCellImHoveringOver.RowIndex).Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _ "'", True) Catch ex As SqlException cellErrorText = "Can't look this cell up" cellErrorLocation = theCellImHoveringOver End Try End Sub Private Sub dataGridView1_CellErrorTextNeeded(ByVal sender As Object, _ ByVal e As DataGridViewCellErrorTextNeededEventArgs) _ Handles dataGridView1.CellErrorTextNeeded If (Not cellErrorLocation Is Nothing) Then If e.ColumnIndex = cellErrorLocation.ColumnIndex AndAlso _ e.RowIndex = cellErrorLocation.RowIndex Then e.ErrorText = cellErrorText End If End If End Sub Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable If resetUnsharedCounter Then ResetCounter() End If ' Alter the data source as necessary Dim adapter As New SqlDataAdapter(query, _ New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _ "Initial Catalog=Northwind;Data Source=localhost")) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture adapter.Fill(table) Return table End Function Private count As New Label() Private unsharedRowCounter As Integer Private Sub ResetCounter() unsharedRowCounter = 0 count.Text = unsharedRowCounter.ToString() End Sub
System.EventArgs
System.Windows.Forms.DataGridViewCellEventArgs
System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventArgs
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
DataGridViewCellContextMenuStripNeededEventArgs MembersSystem.Windows.Forms Namespace
DataGridView Class
DataGridView.CellContextMenuStripNeeded Event
DataGridView.DataSource Property
DataGridView.VirtualMode Property
DataGridView.OnCellContextMenuStripNeeded
DataGridViewCellContextMenuStripNeededEventHandler
DataGridViewCellContextMenuStripNeededEventArgs.ContextMenuStrip
DataGridViewCell.ContextMenuStrip Property
DataGridView.RowContextMenuStripNeeded Event
DataGridViewRow.ContextMenuStrip
ContextMenuStrip Class