Solved

DataGridView Control ContextMenuStrip

Posted on 2006-11-01
2
606 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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