【发布时间】:2016-08-18 10:14:26
【问题描述】:
我正在尝试从另一个按钮调用 datagridview 单元格事件方法。
DataGridView单元格双击方法
private void ListDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
ListDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DataGridViewRow row = this.ListDataGridView.Rows[e.RowIndex];
comboBox2.Text = row.Cells[1].Value.ToString();
}
}
这是我调用此方法的按钮
private void button6_Click(object sender, EventArgs e)
{
ListDataGridView_CellDoubleClick(sender, e);
}
我收到错误
错误 3 类型或命名空间名称“DataGridViewCellEventHandler”确实 命名空间“系统”中不存在(您是否缺少程序集 参考?) C:\VisualC#\Projects\DataGridViewApplication\DataGridViewApplication\List.Designer.cs 340 46 DataGridViewApplication
我做了什么:
我将 EventArgs 更改为 DataGridViewCellEventArgs。
private void button6_Click(object sender, DataGridViewCellEventArgs e)
{
ListDataGridView_CellDoubleClick(sender, e);
}
现在我收到错误:
this.button6.Click += new System.EventHandler(this.button6_Click);
错误 3 'button6_Click' 没有重载匹配委托 'System.EventHandler'
C:\VisualC#\Projects\DataGridViewApplication\DataGridViewApplication\List.Designer.cs 340 35 DataGridViewApplication
现在我将按钮事件处理程序代码更改为此
this.button6.Click += new System.DataGridViewCellEventHandler(this.button6_Click);
仍然收到此错误并卡在这里
错误 3 'button6_Click' 没有重载匹配委托 'System.EventHandler'
在这里找到了解决方案: How to call a datagridview event with a click of a button?
private void button6_Click(object sender, EventArgs e)
{
ListDataGridView_CellDoubleClick(null, null);
}
但这对我不起作用,它给了我一个错误。
对象引用未设置为对象的实例。
【问题讨论】:
-
作为另一种选择,而不是尝试调用
CellDoubleClick的事件处理程序,您可以将逻辑放在像DoSomething这样的方法中,并在您的CellDoubleClick事件和Click事件中调用它按钮。例如,看看this post。
标签: c# datagridview