问题描述
我在datagridview中有一个按钮单元格.当点击那个按钮时,另一个datagridview应该是可见的.对于按钮列中的每个按钮点击,新datagridview中的数据应该不同.我不知道如何实现按钮每一行都不同的点击事件.请帮助我提供示例代码.
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.
推荐答案
您不能为 DataGridViewButtonColumn
中的按钮单元实现按钮单击事件.相反,您使用 DataGridView 的 CellClicked
事件并确定是否为 DataGridViewButtonColumn
中的单元格触发了该事件.使用事件的 DataGridViewCellEventArgs.RowIndex
属性找出点击了哪一行.
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);
}
}
有关 DataGridViewButtonColumn 类的 MSDN 文档 有一个更完整的例子.
The MSDN documentation on the DataGridViewButtonColumn class has a more complete example.
这篇关于datagridview中的按钮点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!