Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 828
pavan Avatar asked Dec 10 '22 05:12

pavan


2 Answers

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
   }
}
like image 135
Rami Alshareef Avatar answered Dec 11 '22 18:12

Rami Alshareef


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);
like image 35
Manzoor Avatar answered Dec 11 '22 18:12

Manzoor