Solved

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

Posted on 2006-07-08
1
310 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:groovyjon
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 17065152
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];
     }
  }

Bob
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now