DataGridVew CellMouseDown not fired first time after dgv is loaded

bdam

Member
Joined
Jan 13, 2010
Messages
6
Programming Experience
5-10
I have a datagridview with both CellMouseDown and RowEnter event handlers. I use the CellMouseDown handler to detect if a header row or a data row was selected and which mouse button was used. If the user left clicks on a data row nothing really happens in CellMouseDown and the RowEnter event is fired which I use to populate an information pane for that row. The DGV is populated by manually creating a data table and setting it as the data source. As part of the data source loading process I set the current cell/row to be the first row.

The problem I am having is that after the data source is updated the very first mouse click in the DGV does not fire the CellMouseDown event. So if I select the last row the CellMouseDown doesn't fire, the RowEnter event fires with the DataGridViewCellEventArgs.RowIndex of zero every time, and the DGV leaves the first row selected. The second time I click it everything fires in the correct order (CellMouseDown, RowEnter) and it works as expected. To be absolutely clear, I am using the passed inDataGridViewCellEventArgs argument to retrieve the row index, I am not using the DGV's CurrentRow property.

In short, after setting the data source the first mouse click always appears to be the first row in the RowEnter handler regardless of what row was actually clicked upon.

Any ideas?

Thanks,
Bryan
 
Sure, it's all OSS and hosted on SourceForge.

The DGV is called _dgvMain and the relevant files are MainForm.Designer.vb and MainForm.vb. The DGV has five events: dgvMainCellMouseDown, dgvMainRowEnter, gvMainSorted, dgvMainKeyUp, and dgvMainLeave. The DGV gets loaded in variety of places for different reasons but one example can be found in BgwUpdateListRunWorkerCompleted.

Thanks,
Bryan
 
When assigning a new data source you will get a RowEnter event on first row, which is selected by default. This happens even if you programmatically de-select or select another right afterwards.
If you focus a different control, for example a button, and then focus back to DGV you will get a RowEnter event on current row. This is probably the event you are detecting before the CellMouseDown and RowEnter event for new cell/row.
There is no missing CellMouseDown though, not that I can see.
 
Ah yes; that seems to be it John. I am handling and ignoring the RowEnter triggered when the data source is assigned using a global boolean that surrounds the assignment calls. However, I didn't realize that entering the DGV itself would trigger a RowEnter call.

So it would seem that I just shouldn't use RowEnter to try and capture when a user is selecting row(s) and rely on CellMouseDown and KeyUp instead to load the relevant data. Where I need to programmatically select the row I can just create a routine that selects the row index and loads the relevant data into the pane.

Thanks again.
 
So it would seem that I just shouldn't use RowEnter to try and capture when a user is selecting row(s) and rely on CellMouseDown and KeyUp instead to load the relevant data. Where I need to programmatically select the row I can just create a routine that selects the row index and loads the relevant data into the pane.
I think I would rather use RowEnter and just ignore the first event after DGV.Enter event occurs.
 
Back
Top