datagridview中的按钮单击事件

sai*_*hma 4 datagridview button

我在datagridview中有一个按钮单元.当单击该按钮时,应该可以看到另一个datagridview.对于按钮列中的每个按钮单击,新datagridview中的数据应该是不同的.我不知道如何实现按钮单击事件每行都有所不同.请帮我查看示例代码.

Jay*_*ggs 7

您无法为a中的按钮单元实现按钮单击事件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);
    }
}
Run Code Online (Sandbox Code Playgroud)

DataGridViewButtonColumn类上的MSDN文档有一个更完整的示例.

  • 我会使用`dataGridView1_CellContentClick`.这样,只有单击按钮本身,而不是填充空间才会出现这种情况. (8认同)