简体   繁体   中英

System.EventArgs Doesn't contain the definition of Button - C#

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?)

在此处输入图片说明

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.


PS: 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

Your delegate handler method should be using DataGridViewCellMouseEventArgs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM