datagridview中的按钮单击事件 [英] button click event in datagridview

查看:125
本文介绍了datagridview中的按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您不能实现按钮单击事件对于 DataGridViewButtonColumn 中的按钮单元格。而是使用DataGridView的 CellClicked 事件,并确定您的 DataGridViewButtonColumn 中的单元格是否触发了该事件。使用事件的 DataGridViewCellEventArgs.RowIndex 属性来找出被点击的行。

  private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e){
//忽略不在我们
中的点击if(e.ColumnIndex == dataGridView1.Columns [MyButtonColumn]。Index&& amp ; e.RowIndex> = 0){
Console.WriteLine(Button on row {0} clicked,e.RowIndex);
}
}

DataGridViewButtonColumn类有一个更完整的例子。


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.

解决方案

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.

这篇关于datagridview中的按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆