How to select a cell in a DataGridView as well as show context menu when right click

I have added a context menu to my DataGridView which appears when right-clicking on a cell. But how do I also select that cell as well as pop up the menu when it is right-clicked? I've tried using the MouseClick and Click events but they don't seem to be called when a context menu is present.
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
Add a CellMouseDown handler like this:

  private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
     if (e.Button == MouseButtons.Right)
       // Set the current cell to the one right-clicked on.
       DataGridView.HitTestInfo hit = this.dataGridView1.HitTest(e.X, e.Y);
       this.dataGridView1.CurrentCell = this.dataGridView1[e.ColumnIndex, e.RowIndex];

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.