DataGridViewButtonColumn 的鼠标事件
时间: 2024-06-18 18:02:52 浏览: 13
DataGridViewButtonColumn 是 DataGridView 控件中的一个列类型,它允许在单元格中显示按钮。 DataGridViewButtonColumn 有以下三个鼠标事件:
1. CellContentClick:当用户单击单元格内的按钮时引发此事件。
2. CellContentDoubleClick:当用户双击单元格内的按钮时引发此事件。
3. CellMouseDown:当用户在单元格内按下鼠标按钮时引发此事件。
你可以使用这些事件来执行特定的操作,比如打开一个对话框、调用一个方法等等。下面是一个示例:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
// 获取所单击的按钮单元格
DataGridViewButtonCell cell = (DataGridViewButtonCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
// 获取该单元格的值
string buttonText = cell.Value.ToString();
// 执行特定操作
if (buttonText == "Button 1")
{
// 打开一个对话框
MessageBox.Show("Button 1 clicked");
}
else if (buttonText == "Button 2")
{
// 调用一个方法
MyMethod();
}
}
}
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)