Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView - Automatically scrolls to top of grid on mouse click

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
US
I have a real strange problem with a DataGridView in a windows form programming in vb.net in VS 2015 Professional.

It centers around what happens with the clicking of a cell in a row. Most of the time, doing so results in the row being selected and its position in the grid remaining as it is without the DataGridView doing any scrolling. But some times (maybe as much as 50% of the time), clicking the cell results in very strange behavior.
1. The row is selected.
2. The DataGridView scrolls up to the first row of the grid. I can then scroll down to the row I clicked on and it is selected.
3. The MouseClick event fires.
4. The CellClick event doesn't fire.

The big question is, "What the heck is going on?". How can a row get selected without the CellClick event firing? And what makes the thing scroll to the top of the grid???

This really screws up the users!!

As a workaround, I am doing a very klugey thing. I have a Boolean flgGotCellClick that only gets set to True in the CellClick event. I monitor that flag in the MouseClick event and if it is false, I retrieve the CurrentCell.rowIndex of the grid and set the FirstDisplayedScrollingRowIndex property to it. It then clears the flag. That is klugey because the end result is the just-selected row becomes visible at the top of the grid. While that is better than going all the way to the top of the grid, it's still a bad behavior.

I've tried setting the SelectionMode property to CellSelect, FullRowSelect, and RowHeaderSelect and it doesn't matter.

Does anyone have a fix for this?

Thanks in advance,
Jerry

Jerry Scannell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top