DataGridView Control ContextMenuStrip

Posted on 2006-11-01
Last Modified: 2008-02-01
I'm using visual studio 2005 and I've created a datagridview control with a contextMenuStrip control associated with it.

My problem is that I need to know what cell the user clicked on (All I really need is the row) so that I can process what the user clicked on in the context menu properly.

This is the prototype for one of the handlers I get:

private void openDetailsToolStripMenuItem_Click(object sender, EventArgs e)

how would I go about retrieving the row number in the datagridview that the user right clicked on to trigger the context menu?

I know of a way to solve this problem in virtual mode, but due to the way data is managed, and the fact that this is a relativly small set of data being displayed, I would prefer not to have to write extra handlers for celldataneeded etc.

Thank you in advance for your reply :)
Question by:GaryJoe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 17856933
You could probably use the "SelectedRows" property of the datagridview, and check that only one row is selected, or loop through each one, all according to what you plan to do...

Int32 selectedRowCount = YourDataGridView.Rows.GetRowCount(DataGridViewElementStates.Selected);
if (selectedRowCount == 1) {
  YourDataGridView.SelectedRows[0].Cells[0].Value.ToString();  //will retrieve the value of the cell in then leftmost column in the first selected row

Accepted Solution

redpipe earned 125 total points
ID: 17857009
Sorry I was a bit hasty posting the previous comment. The easiest way to retrieve the current row number is of course:

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 43
How to escape alias names for Oracle query 1 41
aspx ascx, c# 7 43
Returning multiple values with a stored procedure 9 31
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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