区分DataGridViewButtonColumn和DataGridViewTextColumn单元格点击事件

本文关键字:事件 单元格 DataGridViewTextColumn DataGridViewButtonColumn 区分 | 更新日期: 2023-09-27 17:49:37

我在datagridview控件中有4列,其中第二列是datagridviewtext列,而第三列&第四列是datagridviewbutton列。如何触发不同的事件为datagridviewbutton点击和不同的事件为datagridviewtext列,因为它来在datagirdview cellcontent点击。或任何其他方式查找按钮或文本点击在datagridview单元格内容点击

区分DataGridViewButtonColumn和DataGridViewTextColumn单元格点击事件

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
}

其他单元格类型也一样。