Datagridview cellcontent点击事件不能正常工作
本文关键字:常工作 工作 不能 cellcontent 事件 Datagridview | 更新日期: 2023-09-27 18:10:21
我编写了一个事件来检索datagridview
中CellContentClick
事件中单击的单元格行的第一个单元格值。但是只有当我点击第三个单元格时事件才会引发而当我点击datagridview的第一个或第二个单元格时事件不会引发。
尝试实现CellClick
事件而不是CellContentClick
事件
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView theGrid = sender as DataGridView;
if(theGrid != null)
{
DataGridViewCell selectedCell = theGrid.SelectedCells[0];
//Do your logic here
}
}
为了添加Rami的答案,您还需要更新表单Designer.cs
中的默认生成代码。
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
改为:
this.dataGridView1.*CellClick* += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);