Visual marker when moving rows on DataGridView
.net, c#, datagridview, drag-and-drop, winforms
Solution
I did this for a treeview a couple years ago; can't remember exactly how, but consider using the `MouseMove` event of the DataGridView.
While the drag is occurring, your MouseMove handler should:
- get the relative coordinates of the mouse (the MouseEventArgs contains the coordinates, but I think they're screen coordinates, so you can use `DataGridView.PointToClient()` to convert them to relative)
- determine which row is at that X position (is there a method for this? If not, you can calculate it by adding up the row + row header heights, but remember that the grid may have been scrolled)
- highlight that row or darken its border. One way you may be able to darken one border is by changing the `DataGridViewRow.DividerHeight` property.
- when the mouse moves outside that row, restore it to how it previously looked.
If you wanted to do something custom with the appearance of the row under the mouse (instead of just using the available properties), you can use the `DataGridView.RowPostPaint` event. If you implement a handler for this event which is only used when a row is being dragged over another row, you can repaint the top or bottom border of the row with a bolder brush. MSDN example here.
Problem
Users drag rows up and down in my DataGridView. I have the dragging logic down-pat, but I'd like there to be a dark marker indicating where the row will be placed after I let go of the mouse. Example from Microsoft Access http://img718.imageshack.us/img718/8171/accessdrag.png Example from Microsoft Access; I want to drag rows instead of columns Does anyone know how I'd go about doing this? Is this built-in, or would I have to draw my own marker (if so, how do I do that)? Thanks!