Solved

DataGridView Control ContextMenuStrip

Posted on 2006-11-01
2
597 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

11 Experts available now in Live!

Get 1:1 Help Now