I am having a button cell in datagridview.When that button is clicked,another datagridview should be visible .For every button click in the button column,the data in new datagridview should be differed.I dont know how to implement the button click event which differs for every row.Please help me with the sample code.
Asked
Active
Viewed 3.3k times
5
-
Possible duplicate of [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) – JumpingJezza Jul 16 '19 at 08:33
2 Answers
7
You can't implement a button clicked event for button cells in a DataGridViewButtonColumn
. Instead, you use the DataGridView's CellClicked
event and determine if the event fired for a cell in your DataGridViewButtonColumn
. Use the event's DataGridViewCellEventArgs.RowIndex
property to find out which row was clicked.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
// Ignore clicks that are not in our
if (e.ColumnIndex == dataGridView1.Columns["MyButtonColumn"].Index && e.RowIndex >= 0) {
Console.WriteLine("Button on row {0} clicked", e.RowIndex);
}
}
The MSDN documentation on the DataGridViewButtonColumn class has a more complete example.

Jay Riggs
- 53,046
- 9
- 139
- 151
-
8I would use `dataGridView1_CellContentClick`. That way it would only occur if the button itself has been clicked, but not the padding space. – Igor Feb 20 '13 at 09:49
4
use dataGridView1_CellContentClick instead of dataGridView1_CellClick
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 8) //make sure button index here
{
//write your code here
}
}

Ramgy Borja
- 2,330
- 2
- 19
- 40