我正在使用C#开发一个windows应用程序。我正在使用DataGridView
显示数据。我在其中添加了一个按钮列。我想知道如何在DataGridView
中处理该按钮上的单击事件。
发布于 2010-08-27 00:38:17
这是对WinForms的完全回答:DataGridViewButtonColumn Class
这里:How to: Respond to Button Events in a GridView Control
对于Asp.Net,这取决于您实际使用的控件。(您的问题是DataGrid,但您正在开发一个Windows应用程序,因此您将在那里使用的控件是一个DataGridView...)
发布于 2012-06-12 01:10:07
下面是更好的答案:
不能为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);
}
}
发布于 2010-08-28 15:08:11
这解决了我的问题。
private void dataGridViewName_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Your code
}
https://stackoverflow.com/questions/3577297
复制相似问题