pavan
pavan

Reputation: 143

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.

Upvotes: 4

Views: 9899

Answers (2)

Manzoor
Manzoor

Reputation: 31

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);

Upvotes: 3

Rami Alshareef
Rami Alshareef

Reputation: 7170

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

Upvotes: 11

Related Questions