Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the <code>CellMouseDown</code> event on the <code>DataGridView</code>. From the event handler arguments you can determine which cell was clicked. Using the <code>PointToClient()</code> method on the DataGridView you can determine the relative position of the pointer to the DataGridView, so you can pop up the menu in the correct location.</p> <p>(The <code>DataGridViewCellMouseEvent</code> parameter just gives you the <code>X</code> and <code>Y</code> relative to the cell you clicked, which isn't as easy to use to pop up the context menu.)</p> <p>This is the code I used to get the mouse position, then adjust for the position of the DataGridView:</p> <pre><code>var relativeMousePosition = DataGridView1.PointToClient(Cursor.Position); this.ContextMenuStrip1.Show(DataGridView1, relativeMousePosition); </code></pre> <p>The entire event handler looks like this:</p> <pre><code>private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { // Ignore if a column or row header is clicked if (e.RowIndex != -1 &amp;&amp; e.ColumnIndex != -1) { if (e.Button == MouseButtons.Right) { DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex]; // Here you can do whatever you want with the cell this.DataGridView1.CurrentCell = clickedCell; // Select the clicked cell, for instance // Get mouse position relative to the vehicles grid var relativeMousePosition = DataGridView1.PointToClient(Cursor.Position); // Show the context menu this.ContextMenuStrip1.Show(DataGridView1, relativeMousePosition); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload