【问题标题】:Datagridview cellcontent click event not working properlyDatagridview 单元格内容单击事件无法正常工作
【发布时间】:2011-11-29 16:44:43
【问题描述】:

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

【问题讨论】:

  • 请粘贴一些相关代码

标签: c# winforms events datagridview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    要添加到 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);
    

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 2012-03-06
      • 1970-01-01
      • 2012-09-27
      • 2015-09-25
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多