2016-08-18 38 views
0

私は別のボタンからdatagridviewセルイベントメソッドを呼び出そうとしています。Datagridviewセルのダブルクリックイベントをボタンから呼び出す方法は?

DataGridViewのセルをダブルこれは私が

Error 3 The type or namespace name 'DataGridViewCellEventHandler' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\VisualC#\Projects\DataGridViewApplication\DataGridViewApplication\List.Designer.cs 340 46 DataGridViewApplication

0123を受信して​​います、私はこの方法

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

      } 
     } 

をクリックします

私がしたこと:

EventArgsをDataGridViewCellEventArgsに変更しました。

private void button6_Click(object sender, DataGridViewCellEventArgs e) 
    {  
    ListDataGridView_CellDoubleClick(sender, e); 
    } 

は今、私はエラーを受信して​​います:

this.button6.Click += new System.EventHandler(this.button6_Click); 

Error 3 No overload for 'button6_Click' matches delegate 'System.EventHandler'

C:\VisualC#\Projects\DataGridViewApplication\DataGridViewApplication\List.Designer.cs 340 35 DataGridViewApplication

は今、私はまだここにこのエラーを受信し、stuckedこの

this.button6.Click += new System.DataGridViewCellEventHandler(this.button6_Click); 

にボタンイベントハンドラのコードを変更しました

Error 3 No overload for 'button6_Click' matches delegate '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); 
     } 

をしかし、これは私のために働いていない、それは私にエラーを与えます。

Object Reference not set to an instance of an object.

+0

、あなたはDoSomething''のような方法でロジックを入れて、 'CellDoubleClick'イベントや、ボタンの' Click'イベントの両方でそれを呼び出すことができます。たとえば、[this post](http://stackoverflow.com/a/38611739/3110834)を見てください。 –

答えて

3
private void button6_Click(object sender, EventArgs e) 
    { 

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

    } 
+0

ありがとう、ありがとう。 – Patrick

0

私はあなたがファイルForm.csあなたの一番上にこれを持って期待:

using System.Windows.Forms; 

のEventHandlerは強く型付けされているので、イベントハンドラの両方が一致するタイプを持っている必要があります。

Click EventHandlerはので、あなたの最初のコードが正しいアプローチだった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); 
} 

しかし、あなたはあなたが、2番目のパラメータの正しいタイプ、DataGridViewCellEventArgsを提供する必要が見ることができるようにListDataGridView_CellDoubleClickへの呼び出し。代わりに、 `CellDoubleClick`のイベントハンドラを呼び出そうとする別のオプションとして、

関連する問題