【问题标题】:How to call Datagridview cell double click event from a button?如何从按钮调用 Datagridview 单元格双击事件?
【发布时间】: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


【解决方案1】:
    private void button6_Click(object sender, EventArgs e)
    {

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

    }

【讨论】:

    【解决方案2】:

    使用这个...

    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);
    }
    

    【讨论】:

      【解决方案3】:

      我希望你在 Form.cs 文件的顶部有这个:

      using  System.Windows.Forms;
      

      事件处理程序是强类型的,所以事件和处理程序都需要有匹配的类型。

      Click 事件处理程序需要 void(object sender, 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_CellDoubleClick 时为第二个参数DataGridViewCellEventArgs 提供正确的类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-27
        • 2011-11-09
        • 2015-09-25
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        相关资源
        最近更新 更多