Answered by:
DataGridView.CellClick Event is not firing

Question
-
I have a datagridview. I want to select a row by click a cell on the datagridview. But it is not firing.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { // blah blah }
Thanks.Monday, May 28, 2012 1:31 PM
Answers
-
Hi ardmore,
Did you assign the CellClick event to the dataGridView1_CellClick event handler? For example,
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
Did you override any base method of the form, such as WndProc method?
Have you tried the MouseDown event? Does it raise?
Please try the following code to see if it works and you can download the entire code here.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable dt = new DataTable(); private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.Columns.Clear(); dt.Columns.Add("A"); dt.Columns.Add("B"); dt.Rows.Add("a", "b"); dt.Rows.Add("a1", "b1"); this.dataGridView1.DataSource = dt; this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(string.Format("CellClick row:{0}, column:{1}", e.RowIndex, e.ColumnIndex)); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(string.Format("CellContentClick row:{0}, column:{1}", e.RowIndex, e.ColumnIndex)); } }
Is the code built in debug mode?
In addition, would you mind upload a demo to SkyDrive which will help us to detect the issue?
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Wednesday, May 30, 2012 5:26 AM
All replies
-
One way is to change SelectionMode of DatagridView to FullRowSelect.
Else you can try following code
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { DataGridView1.Rows[e.RowIndex].Selected = true; }
Gaurav Khanna | Microsoft VB.NET MVP
Monday, May 28, 2012 5:54 PM -
Dear MVP: I change the SelectionMode, the code goes to
CellContentClick block rather than CellClick.
Is that okay?
Monday, May 28, 2012 6:00 PM -
I would personally use CellClick - because CellContentClick event will only fire when user will click on the text inside a cell. I prefer not to exactly clicking on a a text, but at least on the whole cell.
And to select a row, you set this property (on a load time):
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
Mitja
Monday, May 28, 2012 6:12 PM -
So how to fire CellClick event? Clicking where? I didn't get it.Monday, May 28, 2012 6:31 PM
-
Any where in each cell - yes - not just on text inside each cell. Try it out by your self. Personally, I dislike CellContentClick event.
public Form1() { this.dataGridView1.CellClick += new DataGridViewCellEventHandler(this.dataGridView1_CellClick); //subscribe this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //set appropriate property } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if(e.RowIndex > -1 && e.ColumnIndex > -1) { dataGridView1.Rows[e.RowIndex].Selected = true; //select } }
Mitja
- Edited by Mitja Bonca Monday, May 28, 2012 6:42 PM
Monday, May 28, 2012 6:37 PM -
Is there any other property affecting the event? It seems to be not working correctly. I clicked any where of the row, but just no response at all.
Even for CellContentClick event, I have to click a few times to enable to fire it.
Monday, May 28, 2012 7:01 PM -
Sure it works. I did an example of using just this simple code:
public Form1() { InitializeComponent(); dataGridView1.Columns.Add("c1", "column1"); dataGridView1.Columns.Add("c2", "column2"); dataGridView1.Rows.Add("a", "b"); dataGridView1.Rows.Add("c", "d"); dataGridView1.Rows.Add("e", "f"); dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { dataGridView1.Rows[e.RowIndex].Selected = true; } }
... and it really does work.
Mitja
Monday, May 28, 2012 7:04 PM -
But it is not working sometimes. When I clicked the cell, the entire row was selected. Please see the image(blue part). Sometimes I had to click the mouse strongly on somewhere. Mouse problem???
Tuesday, May 29, 2012 7:44 PM -
Hello Ardmore,
But it is not working sometimes. When I clicked the cell, the entire row was selected. Please see the image(blue part). Sometimes I had to click the mouse strongly on somewhere. Mouse problem???
If you are managing for the cell content click event? , If so you can select all if you only click the mouse on the text within the cell.
Regards.
Tuesday, May 29, 2012 8:01 PM -
I want to fire CellClick event but it is not fired even I clicked the cell. For the cell content clickevent, it seems like I have to click the text in the cell.Tuesday, May 29, 2012 8:11 PM
-
Hello ardmore,
I want to fire CellClick event but it is not fired even I clicked the cell. For the cell content clickevent, it seems like I have to click the text in the cell.
yeah, I thought you had handled the event click that cell content does not fit what you ask, the event cell click to select the row immediately if you select the cell, see example below.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (dataGridView1 != null) { dataGridView1.SelectedRows[e.RowIndex].Selected = true; } }
Regards.
- Edited by Carmelo La Monica Tuesday, May 29, 2012 8:41 PM
Tuesday, May 29, 2012 8:37 PM -
The question is that I set a breaking point in CellClick event code block, the code never reached there. That means the event was never fired. Why?Tuesday, May 29, 2012 8:45 PM
-
Hi ardmore,
Did you assign the CellClick event to the dataGridView1_CellClick event handler? For example,
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
Did you override any base method of the form, such as WndProc method?
Have you tried the MouseDown event? Does it raise?
Please try the following code to see if it works and you can download the entire code here.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable dt = new DataTable(); private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.Columns.Clear(); dt.Columns.Add("A"); dt.Columns.Add("B"); dt.Rows.Add("a", "b"); dt.Rows.Add("a1", "b1"); this.dataGridView1.DataSource = dt; this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(string.Format("CellClick row:{0}, column:{1}", e.RowIndex, e.ColumnIndex)); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(string.Format("CellContentClick row:{0}, column:{1}", e.RowIndex, e.ColumnIndex)); } }
Is the code built in debug mode?
In addition, would you mind upload a demo to SkyDrive which will help us to detect the issue?
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Wednesday, May 30, 2012 5:26 AM -
ardmore, did you ever get this resolved? I'm having the exact same problem. Row turns blue (obviously the mouse click was recognized) but the event never fired. Click a few times and then it will fire. Very frustrating!
Jim
Friday, April 19, 2019 2:28 PM -
I realize that this is quite far into the future, but I feel obligated to reply with what ended up being the issue for me.
The issue is that I added the code to the Form1.cs as there wasn't a way for me to "generate" the code by clicking on a cell since I am populating DataGridView with a binding list.
I erroneously thought that by adding that code that it would now register the action - not unlike javascript.
Turns out that there was an event property in the datagridview to add that event. Once I did that, it worked fine.
Hard lesson learned, that one in terms of time invested to figure out why my code no worky.
- Edited by James.TM Thursday, August 27, 2020 3:48 PM
Thursday, August 27, 2020 3:14 PM