I'm implementing some drag drop features in one my controls inheriting from a datagridview. Basically I'm dragging a row from somewhere in the DGV and dropping it somewhere else, reordering the rows. I've run into a problem though. If the DGV is too large such that there's a scrollbar, how can I have the DGV scroll up or down while the user is in the middle of a dragdrop?
I know how to get the current mouse position and also get the position of the dgv rectangle and such. So, I can easily find out if i'm in the top or bottom half of the rectangle... I just need a way to programmatically scroll the dgv. I'd prefer if I don't have to keep changing the selected cell to do this.
Any suggestions?
Thanks
Isaac
you can do this by setting
HorizontalScrollingOffset
/VerticalScrollingOffset
of theDataGridView
to set HorizontalScrollingOffset
check
DataGridView.HorizontalScrollingOffset Property
and
for
VerticalScrollingOffset
you can use Reflectioninclude namespace
System.Reflection
You need to implement the DragOver event. Check if the mouse is located close to the top or the bottom of the control (use PointToClient). When it is, enable a timer with an interval of ~200 msec. In the Tick event handler scroll the DGV by a row. Disable the timer when the mouse isn't close and after DoDragDrop returns. The user can now easily and intuitively scroll the grid just be hovering near the ends.
Well, since this is a datagridview... Sorry for the 'winforms' in the question... but I could just do this.. scrolling up or down one row.
Scroll up:
Scroll Down:
You've gotta make sure to check that the numbers don't go out of bounds though.
You may do that using WinAPI by sending message to the control telling it to scroll up or down.
Here is the code, I hope it helps:
Now assuming you have a textbox control on your form. You can move it with:
If that classic general solution doesn't work for you. You may want to look at FirstDisplayedScrollingRowIndex Property and change it regarding your mouse position during dragging.