private void dataGrid_MouseMove(object sender, MouseEventArgs e) { if (e.Button.HasFlag(MouseButtons.Left)) { // If the mouse moves outside the rectangle, start the drag. if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y)) { dataGrid.PreventSelectionChange(); // Prepare drag-n-drop data. AppListDragData dragData = new AppListDragData(this); foreach (DataGridViewRow selectedRow in dataGrid.SelectedRows) { dragData.Apps.Add((selectedRow.DataBoundItem as GridViewItem).App); } // Start drag-n-drop operation. if (dataGrid.DoDragDrop(dragData, DragDropEffects.Move) == DragDropEffects.Move) { // In case of successful move clear the moved items from the library. foreach (SteamApp movedApp in dragData.Apps) { gridViewItems.Remove(gridViewItems.First(i => i.App == movedApp)); } // Suppress the automatic selection. dataGrid.ClearSelection(); } } } }
private void dataGrid_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(AppListDragData))) { AppListDragData dragData = (AppListDragData)e.Data.GetData(typeof(AppListDragData)); if (dragData.Origin != this) { e.Effect = DragDropEffects.Move; } } }
private void dataGrid_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(AppListDragData))) { AppListDragData dragData = (AppListDragData)e.Data.GetData(typeof(AppListDragData)); if (dragData.Origin != this) { e.Effect = DragDropEffects.Move; // Determine an insert position. // The height offset is applying to attain more natural inserting behavior: // items are insert into nearest line between rows. Point clientPoint = dataGrid.PointToClient(new Point(e.X, e.Y)); int heightOffset = dataGrid.Rows.Count > 0 ? dataGrid.Rows[0].Height / 2 : 0; var hitInfo = dataGrid.HitTest(clientPoint.X, clientPoint.Y + heightOffset); int insertIndex = hitInfo.RowIndex >= 0 ? hitInfo.RowIndex : gridViewItems.Count; // Insert the dragging items into the found position. foreach (SteamApp app in dragData.Apps.Reverse <SteamApp>()) { gridViewItems.Insert(insertIndex, new GridViewItem(app)); } // Select the moved rows. dataGrid.ClearSelection(); for (int i = insertIndex; i < insertIndex + dragData.Apps.Count; ++i) { dataGrid.Rows[insertIndex].Selected = true; } // Change the target library. foreach (SteamApp app in dragData.Apps.Reverse <SteamApp>()) { app.TargetLibrary = Library; } } } }