Solved

DataGridView Control ContextMenuStrip

Posted on 2006-11-01
2
608 Views
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 DO NOT WANT TO RUN IN VIRTUAL MODE ***
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 :)
0
Comment
Question by:GaryJoe
  • 2
2 Comments
 
LVL 8

Expert Comment

by:redpipe
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
}
0
 
LVL 8

Accepted Solution

by:
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:
YourDataGridView.CurrentCell.RowIndex
0

Featured Post

Industry Leaders: 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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

763 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