private void AvailableNotesList_MouseMove(object sender, MouseEventArgs e) { if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { // If the mouse moves outside the rectangle, start the drag. if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y)) { // Proceed with the drag and drop, passing in the list item. DragDropEffects dropEffect = AvailableNotesList.DoDragDrop( AvailableNotesList.Rows[rowIndexFromMouseDown], DragDropEffects.Move); } } }
private void AvailableNotesList_DragDrop(object sender, DragEventArgs e) { // The mouse locations are relative to the screen, so they must be // converted to client coordinates. Point clientPoint = AvailableNotesList.PointToClient(new Point(e.X, e.Y)); // Get the row index of the item the mouse is below. rowIndexOfItemUnderMouseToDrop = AvailableNotesList.HitTest(clientPoint.X, clientPoint.Y).RowIndex; // If the drag operation was a move then remove and insert the row. if (e.Effect == DragDropEffects.Move) { DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow; AvailableNotesList.Rows.RemoveAt(rowIndexFromMouseDown); AvailableNotesList.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove); } }