private void DGV_DragOver(object sender, DragEventArgs e) { var pt = DGV.PointToClient(new Point(e.X, e.Y)); var ht = DGV.HitTest(pt.X, pt.Y); if ((0 <= ht.RowIndex) && (e.Data.GetData(typeof(DownloadRow)) is DownloadRow row) && (row.GetVisibleIndex() != ht.RowIndex)) { e.Effect = e.AllowedEffect; if (_DragOver_RowIndex != ht.RowIndex) { _DragOver_RowIndex = ht.RowIndex; DGV.Invalidate(); } ScrollIfNeed(in pt); return; } e.Effect = DragDropEffects.None; if (_DragOver_RowIndex.HasValue) { _DragOver_RowIndex = null; DGV.Invalidate(); } ScrollIfNeed(in pt); }
private void DGV_MouseMove(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } var row = GetSelectedDownloadRow(); if (row == null) { return; } var ht = DGV.HitTest(e.X, e.Y); if ((ht.RowIndex < 0) || (ht.ColumnIndex < 0)) { return; } if (_DGV_MouseDown_HitTestInfo.Type != DataGridViewHitTestType.Cell) { return; } const int MOVE_DELTA = 5; if (Math.Abs(_DGV_MouseDown_ButtonLeft_Location.X - e.X) < MOVE_DELTA && Math.Abs(_DGV_MouseDown_ButtonLeft_Location.Y - e.Y) < MOVE_DELTA) { return; } //-----------------------------------------------------// _DragOver_RowIndex = null; DGV.AllowDrop = true; DGV.DragOver += DGV_DragOver; DGV.DragDrop += DGV_DragDrop; DGV.CellPainting += DGV_DragDrop_CellPainting; try { var dragDropEffect = DGV.DoDragDrop(row, DragDropEffects.Move); if (dragDropEffect == DragDropEffects.Move) { SelectDownloadRow(row); } else { DGV.Invalidate(); } } finally { DGV.DragOver -= DGV_DragOver; DGV.DragDrop -= DGV_DragDrop; DGV.CellPainting -= DGV_DragDrop_CellPainting; DGV.AllowDrop = false; } }
private void DGV_Scroll(object sender, ScrollEventArgs e) { DGV.Invalidate(); }
private void Model_CollectionChanged(_CollectionChangedTypeEnum_ collectionChangedType) { switch (collectionChangedType) { case _CollectionChangedTypeEnum_.Sort: DGV.Refresh(); break; case _CollectionChangedTypeEnum_.Add: { var v = _UserMade_DGV_SelectionChanged; DGV.RowCount = _Model.RowsCount; RestoreSortIfNeed(); if (v != _UserMade_DGV_SelectionChanged) { _UserMade_DGV_SelectionChanged = false; } _CommonUpdateTimer.Enabled = true; } break; case _CollectionChangedTypeEnum_.Remove: case _CollectionChangedTypeEnum_.Clear: case _CollectionChangedTypeEnum_.BulkUpdate: { #region [.save selected row.] var selectedVisibleIndex = (DGV.SelectedRows.Cast <DataGridViewRow>().FirstOrDefault()?.Index).GetValueOrDefault(-1); #endregion DGV.CellValueNeeded -= DGV_CellValueNeeded; DGV.CellFormatting -= DGV_CellFormatting; DGV.SelectionChanged -= DGV_SelectionChanged; DGV.CellPainting -= DGV_CellPainting; try { DGV.RowCount = _Model.RowsCount; RestoreSortIfNeed(); } finally { DGV.CellValueNeeded += DGV_CellValueNeeded; DGV.CellFormatting += DGV_CellFormatting; DGV.SelectionChanged += DGV_SelectionChanged; DGV.CellPainting += DGV_CellPainting; } #region [.restore selected row.] try { var hasRows = (0 < DGV.RowCount); _CommonUpdateTimer.Enabled = hasRows; if (hasRows) { var visibleIndex = Math.Min(Math.Max(0, selectedVisibleIndex), DGV.RowCount - 1); var dtrow = DGV.Rows[visibleIndex]; if (dtrow.Selected) { var row = _Model[visibleIndex]; SelectionChanged?.Invoke(row); } else { dtrow.Selected = true; } } else { SelectionChanged?.Invoke(null); } } catch (Exception ex) { Debug.WriteLine(ex); } #endregion DGV.Invalidate(); } break; } }