0
最近添加到我的C#应用程序的Toolstrip,并且当点击调用该函数的菜单项时出现错误,但是当从Windowsform Button调用该函数时,它工作得很完美。 以下是我的函数:ToolstripMenuItem点击提示错误?
private void ShowAddDocument(object sender, EventArgs e)
{
foreach (var rr in this.gridEX1.GetDataRows())
{
if (rr.Cells["Select"].Value != null)
{
if (rr.Cells["DocumentNumber"].Value.ToString() != "")
{
rr.BeginEdit();
rr.Cells["Select"].Value = null;
rr.EndEdit();
}
else
{
this.gridEX1.Tag = "SELECTED";
}
}
}
我用下面的线来连接功能和工具条菜单项Click事件:
this.userControl11.IssueDocMenuItem.Click += new System.EventHandler(ShowAddDocument);
但是当点击工具条菜单项也引发错误: “对象引用未设置为对象的实例“。 但奇怪的部分是,当我使用Windowsform按钮和下面的代码调用该函数:
private void button1_Click(object sender, EventArgs e)
{
ShowAddDocument(sender, e);
}
据我的代码工作没有任何错误!!!
它给出了哪一行?我只能根据你的代码做出假设。它可以是'gridEX',也可以是null,'GetDataRows'返回null,使'rr'为空,或者其中一个单元格检索'rr'上的索引。 'rr [“Select”]''''rr [“DocumentNumber”]'另请参阅:http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-它 – Bauss
没有我的网格不为空,当点击Button1时它没有任何错误 –
是的,因为一个已经开发了多年的运行时突然在这样简单的代码中随机抛出空值的异常?不要听起来粗鲁或任何事情,但你应该*总是*假设你自己的代码是不正确的。您是否尝试使用调试器检查* ALL *值,而不仅仅是您的网格,还有您的方法/行索引中返回哪些值。 – Bauss