C# 如何从按钮调用Datagridview单元格双击事件?

C# 如何从按钮调用Datagridview单元格双击事件?,c#,datagridview,C#,Datagridview,我试图从另一个按钮调用datagridview单元格事件方法 DataGridView单元格双击方法 private void ListDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { ListDataGridView.SelectionMode =

我试图从另一个按钮调用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);
}
这是我调用此方法的按钮

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”不正确 命名空间“System”中不存在(是否缺少程序集 参考?)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\u 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\u Click”没有重载与委托匹配 'System.EventHandler'

在这里找到了解决方案:

但这对我不起作用,它给了我一个错误

对象引用未设置为对象的实例


我希望您在Form.cs文件的顶部有以下内容:

using  System.Windows.Forms;
EventHandler是强类型的,因此事件和处理程序都需要有匹配的类型

Click
eventhandler需要方法签名
void(objectsender,EventArgs e)
,因此您的初始代码是正确的方法:

private void button6_Click(object sender, EventArgs e)
{
  // create and set values for the event argument. 
  // it can't be EventArgs, so just instantiate the right type
  // the constructor needs a row and column
  var datagridviewArgs = new DataGridViewCellEventArgs(42,13);
  ListDataGridView_CellDoubleClick(sender, datagridviewArgs);
}
但是正如您所看到的,您需要为第二个参数提供正确的类型,即调用
ListDataGridView\u CellDoubleClick

用这个

    private void button6_Click(object sender, EventArgs e)
    {

        ListDataGridView_CellDoubleClick(this.ListDataGridView, new DataGridViewCellEventArgs(this.ListDataGridView.CurrentCell.ColumnIndex,this.ListDataGridView.CurrentRow.Index));

    }
private void btnChoose_Click(object sender, EventArgs e)
{

    MouseEventArgs b = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 2, 
        MousePosition.X, MousePosition.Y, 0);
    DataGridViewCellMouseEventArgs a = new DataGridViewCellMouseEventArgs(0, 0, 
        MousePosition.X, MousePosition.Y, b);
    dataGridView1_CellMouseDoubleClick(sender, a);
}

作为另一个选项,您不必尝试调用
CellDoubleClick
的事件处理程序,您可以将逻辑放入类似
DoSomething
的方法中,并在按钮的
CellDoubleClick
事件和
Click
事件中调用它。例如,看一看。