Datagridview cellcontent点击事件不能正常工作

本文关键字:常工作 工作 不能 cellcontent 事件 Datagridview | 更新日期: 2023-09-27 18:10:21

我编写了一个事件来检索datagridviewCellContentClick事件中单击的单元格行的第一个单元格值。但是只有当我点击第三个单元格时事件才会引发而当我点击datagridview的第一个或第二个单元格时事件不会引发。

Datagridview cellcontent点击事件不能正常工作

尝试实现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);