0

I have a DataGridView on my winform and I am showing 5 columns and few rows in it, I want to add a functionality that if I right click on any row it displays me a menu to see more detail about that record. But I am getting an error when I write e.Button that

System.EventArgs Doesn't contain the definition of Button and no extension method button accepting the first argument of type System.EventArgs could be found (are you missing any directive or an assembly reference?)

enter image description here

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • 1
    Button is in MouseEventArgs. See: http://stackoverflow.com/questions/19448346/how-to-get-a-right-click-mouse-event-changing-eventargs-to-mouseeventargs-cause – Dan Orlovsky Mar 15 '16 at 13:56

2 Answers2

2

The MouseClick event of DataGridView actually provides a MouseEventArgs argument. You declared your datagridview1_MouseClick method only with EventArgs.

Change that to

protected void datagridview1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == ...

and it should work (in case you used CellMouseClick instead of MouseClick, use DataGridViewCellMouseEventArgs instead of MouseEventArgs).


As a side note: you don't get a compilation error when you add your "wrongly" declared handler like datagridview1.MouseClick += datagridview1_MouseClick, because MouseEventArgs is derived from EventArgs, so the compiler has no problem with that assignment. The problem arises when you try to access the properties of a MouseEventArgs instance via e when e is declared as EventArgs, because EventArgs doesn't know the properties of the MouseEventArgs derivate.


P.S.: Please post your code as text to your next question. It's better for us to read or to re-use for reproducing the error than an image. In that image, I can't see if you used the MouseClick or CellMouseClick event

René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

Your delegate handler method should be using DataGridViewCellMouseEventArgs

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellmouseclick(v=vs.110).aspx

James Dev
  • 2,979
  • 1
  • 11
  • 16