[SOLVED] Datagridview dragDrop move rows

Answered Closed
0
0

How do I implement DragDrop for a datagridview to move rows within the datagridview? I tried, but in the DragEnter and DragDrop Event I only get x and y positions of the mouse. So how can I get the row the mouse is currently over. Or ist there another way dragdrop has to be done?

  • You must to post comments
Best Answer
0
0

Hi Gunter,

please find a drag and drop sample using the datagrid that includes the functionality you need here:

http://wisej.s3.amazonaws.com/support/attachments/Wisej.DataGridDragDrop.zip

Best regards
Frank

  • You must to post comments
0
0

Hi Mark,

the bug was logged as WJ-8140 is fixed in the latest release (1.3.44):

Best regards
Frank

  • Mark Reed
    It is working now thanks :)
  • You must to post comments
0
0

I have updated the sample to use DGV.HitTest() instead of calculating the row. However the null reference exception is caused by a new regression in the DGV occurring when a row is removed and added back. It’s fixed in the the dev build that will be uploaded shortly.

http://wisej.s3.amazonaws.com/support/attachments/Wisej.DataGridDragDrop.zip

  • You must to post comments
0
0

I just tried the sample in this ticket with the latest version and I am getting an object reference exception. Is this still the recommended way to do it? It looks like the error is in the framework.

  • You must to post comments
0
0

Hi Gunter,

We added DataGridView.HitTest() to the current build, coming online later on today.

Best,

Luca

  • You must to post comments
0
0

Hi Frank

Thanks for the code. I missed grid.PointToClient(Control.MousePosition).Y,

There are 2 bugs in the code:

  1. Crash when dropping into area after last row of datagridview
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
...
                if (dropRow == null)
                {
                    grid.Rows.Remove(dragRow);
                    grid.Rows.Add(dropRow);
                }
Should be changed to:
                if (dropRow == null)
                {
                    grid.Rows.Remove(dragRow);
                    grid.Rows.Add(dropRow);
                }
2. Crash when dropping on the column header

private DataGridViewRow GetDragRow(DataGridView grid)

  {
    var rowCount = grid.RowCount;
    var index = grid.FirstDisplayedRowIndex - 1;
 
    var position = grid.PointToClient(Control.MousePosition).Y;
 
    position -= grid.ColumnHeadersHeight;
    if (position < 0)
    {
      position = 1;
    }
    while (position > 0 && index < rowCount)
    {
      index++;
      if (grid.Rows[index].Visible)
        position -= grid.Rows[index].Height;
    }
 
    return grid.Rows[index];
  }
}
In this case the index = 1- and position < 0, so the while loop will never be executed,
the statement
return grid.Rows[index];
 crashes, because index = -1
can be changed to 
position -= grid.ColumnHeadersHeight;
if (position < 0)
{
  position = 1;
}
The position is moved to the first visible row, an the drop operation is done on the is row.

 

  • You must to post comments
Showing 6 results