1

I'm working in visual studio and trying to get information from a DataGridView cell when the user double clicks on it. I basically set up the CellDoubleClick event just like any other Click event but that doesn't seem to work.

Code:

Form1.cs

private void dataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
    {

        System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
        messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex);
        messageBoxCS.AppendLine();
        messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex);
        messageBoxCS.AppendLine();
        MessageBox.Show(messageBoxCS.ToString(), "CellDoubleClick Event");
    }

Pertinent code in Form1.Designer.cs

this.dataGridView1.CellDoubleClick += new System.EventHandler(this.dataGridView1_CellDoubleClick);

I am getting an error in the Form1.Designer code that says, "No overload for 'dataGridView1_CellDoubleClick' matches delegate 'System.EventHandler'.

How can I get the double click to work correctly? Thanks.

Rupert
  • 75
  • 1
  • 3
  • 7

1 Answers1

2

The CellDoubleClick event is a DataGridViewCellEventHandler, not an EventHandler`.

You should add the event handles using the designer, which will automatically use the correct delegate type.
You should not edit the designer-generated code manually.

In general, when adding event handlers, you shouldn't explicitly create the delegate.
Instead, you can write

myGrid.CellDoubleClick += MyGrid_CellDoubleClick;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ok, I'm not terribly good at Visual Studio and I was kind of at my wits end trying to get double click to work. After I read your comment I looked closer at the designer and found the events button. Now it works. Thanks. – Rupert Nov 23 '10 at 02:51
  • If you have time , please explain to me as if i were 5 years old why it's not possible to simply do += new EventHandler(MyGrid_CellDoubleClick); to handle this event, i do not get it. :) – JazzCat Sep 18 '15 at 09:53
  • @JazzCat: Because that event is declared as a different type. https://msdn.microsoft.com/en-us/library/ms173171 – SLaks Sep 18 '15 at 14:33