Solved

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

Posted on 2006-07-08
1
304 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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

12 Experts available now in Live!

Get 1:1 Help Now