DGV DragDrop - the line disappears

I am trying to write some code so that users of my application can drag and drop rows into a DataGridView to reorder them. The problem is that the row that is being dragged disappears when it is discarded, so dragging and dropping results in a simple deletion of that row. Here is my code:

private Rectangle dragBoxFromMouseDown; private int rowIndexFromMouseDown; private int rowIndexOfItemUnderMouseToDrop; private void grdCons_MouseMove(object sender, MouseEventArgs e) { if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(eX, eY)) { DragDropEffects dropEffect = grdCons.DoDragDrop(grdCons.Rows[rowIndexFromMouseDown], DragDropEffects.Move); } } } private void grdCons_MouseDown(object sender, MouseEventArgs e) { rowIndexFromMouseDown = grdCons.HitTest(eX, eY).RowIndex; if (rowIndexFromMouseDown != -1) { Size dragSize = SystemInformation.DragSize; dragBoxFromMouseDown = new Rectangle(new Point(eX - (dragSize.Width / 2), eY - (dragSize.Height / 2)), dragSize); } else { dragBoxFromMouseDown = Rectangle.Empty; } } private void grdCons_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void grdCons_DragDrop(object sender, DragEventArgs e) { Point clientPoint = grdCons.PointToClient(new Point(eX, eY)); rowIndexOfItemUnderMouseToDrop = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex; if (e.Effect == DragDropEffects.Move) { DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow; grdCons.Rows.RemoveAt(rowIndexFromMouseDown); grdCons.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove); } } 

Guess the Insert on DGV in the DragDrop event is not working.

+4
source share
2 answers

Here is a cleaned version of your code that works:

  public Form1() { InitializeComponent(); grdCons.Rows.Add(7); for (int i = 0; i < grdCons.Rows.Count; i++) { grdCons.Rows[i].Cells[0].Value = i; grdCons.Rows[i].Cells[1].Value = "Cell " + i; } grdCons.AllowDrop = true; grdCons.AllowUserToAddRows = false; grdCons.AllowUserToDeleteRows = false; grdCons.MouseMove += new MouseEventHandler(grdCons_MouseMove); grdCons.MouseDown += new MouseEventHandler(grdCons_MouseDown); grdCons.DragOver += new DragEventHandler(grdCons_DragOver); grdCons.DragDrop += new DragEventHandler(grdCons_DragDrop); } private int rowIndexFromMouseDown; private void grdCons_MouseMove(object sender, MouseEventArgs e) { if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { grdCons.DoDragDrop(grdCons.Rows[rowIndexFromMouseDown], DragDropEffects.Move); } } private void grdCons_MouseDown(object sender, MouseEventArgs e) { rowIndexFromMouseDown = grdCons.HitTest(eX, eY).RowIndex; } private void grdCons_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void grdCons_DragDrop(object sender, DragEventArgs e) { Point clientPoint = grdCons.PointToClient(new Point(eX, eY)); int targetIndex = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex; if (e.Effect == DragDropEffects.Move) { DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow; grdCons.Rows.RemoveAt(rowIndexFromMouseDown); grdCons.Rows.Insert(targetIndex, rowToMove); } } 
0
source

The task lies in grdCons_DragDrop() . Since you mentioned that the DGV is bound to a DataTable call, calling grdCons.Rows.Insert(targetIndex, rowToMove) , an InvalidOperationException is InvalidOperationException . When the DGV is data bound, you need to manage the DataSource , not the DGV. Here is the correct way to call grdCons_DragDrop() .

 private void grdCons_DragDrop(object sender, DragEventArgs e) { DataTable tbl = (DataTable)grdCons.DataSource; Point clientPoint = grdCons.PointToClient(new Point(eX, eY)); int targetIndex = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex; if (e.Effect == DragDropEffects.Move) { DataRow row = tbl.NewRow(); row.ItemArray = tbl.Rows[rowIndexFromMouseDown].ItemArray; //copy the elements tbl.Rows.RemoveAt(rowIndexFromMouseDown); tbl.Rows.Insert(targetIndex, rowToMove); } } 
0
source

Source: https://habr.com/ru/post/1381921/


All Articles