title: C# DataGridView Right Click to ContextMenu Click Retrieve Cell Value
body: <p>I have a DataGridView.  I have created a ContextMenuStrip when Right Clicking a Cell in column 4 of my DataGridView.  I am however stuck because on left clicking the ContextMenuStrip Menu Item I would like the extract the data from the cell that was right clicked. </p>
  
  <p>The cell I want is the top left corner of the ContextMenuStrip which is precisely where I right clicked and points at the cell who's data I want to grab.  <a href="https://i.imgur.com/d2WhXFc.jpg" rel="nofollow noreferrer">The screen grab just doesn't show the mouse cursor.</a></p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>GridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown);
  
  private void dataGridView_MouseDown(object sender, MouseEventArgs e)
      {
  
          if (e.Button == MouseButtons.Right)
          {
              var ht = dataGridView1.HitTest(e.X, e.Y);
  
              //Checks for correct column index
              if (ht.ColumnIndex == 4 &amp;&amp; ht.RowIndex != -1)
              {
                  //Create the ContextStripMenu for Creating the PO Sub Form
                  ContextMenuStrip Menu = new ContextMenuStrip();
                  ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
                  MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
                  Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
  
                  //Assign created context menu strip to the DataGridView
                  dataGridView1.ContextMenuStrip = Menu;
              }
  
              else
                  dataGridView1.ContextMenuStrip = null;
          }
      }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/a/5526193/2157661">I think this post may be what I am looking for</a></p>
  
  <p>However if I change: <code>private void dataGridView_MouseDown(object sender, MouseEventArgs e)</code>
  to</p>
  
  <p><code>private void dataGridView_MouseDown(object sender, DataGridViewCellMouseEventArgs e)</code></p>
  
  <p>I am not sure how to change <code>GridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown);</code> So I don't get an error message.  Or is there a better way to do this?</p>
  
  <p><strong><em>Final Solution</em></strong> With help from Gjeltema</p>
  
  <p><code>dataGridView1.CellMouseDown += this.dataGridView1_CellMouseDown;</code></p>
  
  <pre><code>    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
      {
          //Checks for correct column index
          if (e.Button == MouseButtons.Right &amp;&amp; e.ColumnIndex == 4 &amp;&amp; e.RowIndex != -1)
          {
              //Create the ContextStripMenu for Creating the PO Sub Form
              ContextMenuStrip Menu = new ContextMenuStrip();
              ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
              MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
              Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
  
              //Assign created context menu strip to the DataGridView
              dataGridView1.ContextMenuStrip = Menu;
              CellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
          }
  
          else
              dataGridView1.ContextMenuStrip = null;
      }
  </code></pre>