0

I'm writing a program in C# and windows forms and I have a problem.

I have a DataGridView where I display data in different columns and I added a DataGridViewButtonColumn. The thing is that I need to separate the CellClick event on the DataGridViewButtonColumn of the whole DataGridView.CellClick event. To resume I would like to invoke a different function if I click on the button in the datagrid than if I click on the other cells.

Thanks in advance.

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • Does this answer your question? [How to handle click event in Button Column in Datagridview?](https://stackoverflow.com/questions/3577297/how-to-handle-click-event-in-button-column-in-datagridview) – rold2007 Dec 07 '21 at 23:40

1 Answers1

2

From the MSDN article, handle the CellClick event

    dataGridView1.CellClick +=
        new DataGridViewCellEventHandler(dataGridView1_CellClick);

and from within there, check the column number is what you expect.

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Ignore clicks that are not on button cells.  
    if (e.RowIndex < 0 || e.ColumnIndex !=
        dataGridView1.Columns["Status Request"].Index) return;
    ....
}

Obviously substitute your own column name for "Status Request", or just use the index value. There's no way (to my knowledge) to attach an event to the button directly, as the button it embeds isn't an actual "Button" object but rather just a GUI trick to make it look like one, so the only thing you can do is check for the column index.

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90