C# – Visual C# – Associate event handler with CellDoubleClick event

cdatagridviewoverloading

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.

Best Answer

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;
Related Topic