Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Mouse coordinates in CellMouseClick event
 

Mouse coordinates in CellMouseClick event

Hi

I want to display different ContextMenuStrips depending in which cell the user right-clicks. Since I'm using an unbound dgv I can't use the ContextMenuStripNeeded event (is this correct?) so I'm trying to just simple show the ContextMenuStrip programatically. My problem is that the DataGridViewCellMouseEventArgs only return the location of the mouse with the current cell not within the dgv itself. Is there any way of getting the mouse position within the dgv when using the CellMouseClick event?

Cheers,
Pedro
pmsc  Monday, September 04, 2006 2:41 PM
I solved my problem by using the dgv mousedown event and then getting the cell by the HitTestInfo method in the dgv class. I can then use the mouse coordinates from the mousedown event to position the contextmenustrip.
pmsc  Thursday, September 07, 2006 10:23 AM
I dont see a contextmenustripneeded event. I would use the datagridview's cellmousedown event and display the contextmenu with it's show command.
Ken Tucker  Monday, September 04, 2006 2:56 PM
I found this on MSDN: "The CellContextMenuStripNeeded event also occurs whenever the value of the DataGridViewCell.ContextMenuStrip property is retrieved, either programmatically or when the user right-clicks a cell."

If I use the cellmousedown event, will that give me the mouse coordinates with respect to the dgv or with respect to the cell?

Pedro
pmsc  Monday, September 04, 2006 3:24 PM
The cell mouse down event gives you the rowindex, columnindex, mousebutton, x, and y coordinates. I see the CellContextMenuNeeded Event now but it does not seam to work with an unbound datagridview.  The mouse coordinates are in cell.  You can always use the forms or datagridview's  mouseposition to get its location.
Ken Tucker  Monday, September 04, 2006 4:07 PM

Hi,

pmsc wrote:
If I use the cellmousedown event, will that give me the mouse coordinates with respect to the dgv or with respect to the cell?

It is with respect to the cell, as you can simply verify it using this code snippet.



private void cell_mouse_down(object sender, DataGridViewCellMouseEventArgs e)
{
this.label1.Text = e.X.ToString() + "," + e.Y.ToString();
}

gqlu  Tuesday, September 05, 2006 2:16 AM

Hi,

You can simply set the contextstripmenu to the specific cells.

e.g.


this.dataGridView1.Rows[0].Cells[0].ContextMenuStrip = this.contextMenuStrip1;

Change the index accordingly and therefore different contextmenustrip can be set according to the different index.

gqlu  Tuesday, September 05, 2006 2:42 AM
I want the contextmenustrip to depend on the current value in the cell so it must be set at runtime. I tried doing this in the CellMouse Click event:




Private
Sub DataGridView1_CellMouseClick(sender as Object, e as
DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick

DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ContextMenuStrip = ContextMenuStrip1

End Sub




The problem though is that the CellMouseClick event seems to happen after the contextmenustrip is shown. So I have to right click the cell twice, the first time to set the contextmenustrip and the second time to show it.

Does the CellMouseDown event happen before the contextmenustrip is shown?

Pedro
pmsc  Wednesday, September 06, 2006 11:00 AM
I solved my problem by using the dgv mousedown event and then getting the cell by the HitTestInfo method in the dgv class. I can then use the mouse coordinates from the mousedown event to position the contextmenustrip.
pmsc  Thursday, September 07, 2006 10:23 AM

Hello there,

I am actually trying to solve the same problem, capturing the mouse coordinates with respect to the grid view. I was wondering if you'd be able to post your solution(sample code) as to how you solved this problem.

Thanks,

Shail

Shail1  Wednesday, April 04, 2007 9:45 PM

You can use google to search for other answers

Custom Search

More Threads

• Allow User to add rows not working when binding to a List
• Programmatically Edit a GridView?
• New table in Dataset cannot be bound?
• Problem with DataSet Designer
• Datagridview update with thread
• Date Entry with a Data Grid
• Exception while making a cell ReadOnly
• datagrid view
• Dynamic Binding of a BindingNavigator Control in Visual Basic
• DataGridView Combobox after Click