[英]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. 我在winform上有一个DataGridView
,并在其中显示5列和几行,我想添加一个功能,如果我右键单击任何行,它将显示一个菜单,以查看有关该记录的更多详细信息。 But I am getting an error when I write e.Button
that 但是我在写e.Button
时遇到错误
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?) System.EventArgs不包含Button的定义,也没有找到接受System.EventArgs类型的第一个参数的扩展方法button(您是否缺少任何指令或程序集引用?)
The MouseClick
event of DataGridView
actually provides a MouseEventArgs
argument. 实际上, DataGridView
的MouseClick
事件提供了一个MouseEventArgs
参数。 You declared your datagridview1_MouseClick
method only with EventArgs
. 您仅使用EventArgs
声明了datagridview1_MouseClick
方法。
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
). 它应该工作(如果你使用的CellMouseClick
代替MouseClick
,使用DataGridViewCellMouseEventArgs
而不是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. 附带说明:添加“错误”声明的处理程序(例如datagridview1.MouseClick += datagridview1_MouseClick
,不会出现编译错误,因为MouseEventArgs
派生自EventArgs
,因此编译器对该分配没有问题。 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. 当您将e
声明为EventArgs
时,尝试通过e
访问MouseEventArgs
实例的属性时,就会出现问题,因为EventArgs
不知道MouseEventArgs
派生的属性。
PS: Please post your code as text to your next question. PS:请将您的代码作为文本发布到下一个问题。 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 在这个形象,我不能看到,如果你使用的MouseClick
或CellMouseClick
事件
Your delegate handler method should be using DataGridViewCellMouseEventArgs 您的委托处理程序方法应使用DataGridViewCellMouseEventArgs
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellmouseclick(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.windows.forms.datagridview.cellmouseclick(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.