2013-05-01 63 views
0

我正在用C#和Windows窗体编写程序,我遇到了问题。如何分离整个DataGridView的CellClick事件的DataGridViewButtonColumn上的CellClick?

我有一个DataGridView我在不同的列中显示数据,我添加了一个DataGridViewButtonColumn。事情是我需要分开整个DataGridView.CellClick事件的DataGridViewButtonColumn上的CellClick事件。要继续我想调用一个不同的功能,如果我点击数据网格中的按钮比单击其他单元格。

在此先感谢。

回答

2

the MSDN article,处理CellClick事件

dataGridView1.CellClick += 
     new DataGridViewCellEventHandler(dataGridView1_CellClick); 

,并从那里内,检查列数是你所期望的。

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    // Ignore clicks that are not on button cells. 
    if (e.RowIndex < 0 || e.ColumnIndex != 
     dataGridView1.Columns["Status Request"].Index) return; 
    .... 
} 

显然将自己的列名替换为“状态请求”,或者只是使用索引值。根据我的理解,无法直接将事件附加到按钮上,因为它嵌入的按钮不是实际的“按钮”对象,而只是使GUI看起来像一个的GUI技巧,因此您唯一可以做的是检查列索引。

+0

非常感谢Dax Fohl – 2013-05-01 21:29:17