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
Solved

DataGridView Control ContextMenuStrip

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 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