简体   繁体   中英

Datagridview cellcontent click event not working properly

I wrote an event to retrieve the first cell value of the clicked cell's row on CellContentClick event in datagridview . but the event is getting raised only when i click the third cell and not getting raised when i click the first or second cell of datagridview.
Please help me.

Try to implement the CellClick event instead of CellContentClick event

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
   }
}

To add to Rami's answer, you will also need to update the default generated code in your Form's Designer.cs .

Original code:

this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);

Change it to:

this.dataGridView1.*CellClick* += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM