Button datagridview中的按钮单击事件

Button datagridview中的按钮单击事件,button,datagridview,Button,Datagridview,我在datagridview中有一个按钮单元格。单击该按钮时,另一个datagridview应可见。对于按钮列中的每个按钮单击,新datagridview中的数据应该不同。我不知道如何实现按钮单击事件,每行都不同。请帮助我获取示例代码。您不能为DataGridViewButtonColumn中的按钮单元格实现按钮单击事件。相反,您可以使用DataGridView的CellClicked事件,并确定是否为DataGridViewButtonColumn中的单元格触发事件。使用事件的DataGrid

我在datagridview中有一个按钮单元格。单击该按钮时,另一个datagridview应可见。对于按钮列中的每个按钮单击,新datagridview中的数据应该不同。我不知道如何实现按钮单击事件,每行都不同。请帮助我获取示例代码。

您不能为
DataGridViewButtonColumn中的按钮单元格实现按钮单击事件。相反,您可以使用DataGridView的
CellClicked
事件,并确定是否为
DataGridViewButtonColumn
中的单元格触发事件。使用事件的
DataGridViewCellEventArgs.RowIndex
属性找出单击了哪一行

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

上的MSDN文档有一个更完整的示例。

使用dataGridView1\u CellContentClick而不是dataGridView1\u CellClick

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

    if (e.ColumnIndex == 8) //make sure button index here
    {
          //write your code here
    }

}

我会使用
dataGridView1\u CellContentClick
。这样,只有当按钮本身被点击时才会发生,而不是填充空间。可能重复