【问题标题】:Distinguish between DataGridViewButtonColumn and DataGridViewTextColumn cell click event区分 DataGridViewButtonColumn 和 DataGridViewTextColumn 单元格单击事件
【发布时间】:2014-03-12 11:38:26
【问题描述】:
我在 datagridview 控件中有 4 列,其中第 2 列是 datagridviewtext 列,而第 3 和第 4 列是 datagridviewbutton 列。
如何为 datagridviewbutton 单击触发不同的事件,并为 datagridviewtext 列触发不同的事件,因为它同时出现在 datagirdview 单元格内容单击中。
或以任何其他方式在 datagridview 单元格内容单击中查找按钮或文本单击。
【问题讨论】:
标签:
c#
winforms
datagridview
【解决方案1】:
在CellContentClick 事件中,一种方法是检查单元格类型,如下所示:
if (dataGridView1[e.ColumnIndex, e.RowIndex].GetType() == typeof(DataGridViewButtonCell)
{
// handle button cell click
}
else if (dataGridView1[e.ColumnIndex, e.RowIndex].GetType() == typeof(DataGridViewTextBoxCell)
{
// handle textbox cell click
}
其他细胞类型也一样。