You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
Thanks for the code. I missed grid.PointToClient(Control.MousePosition).Y,
There are 2 bugs in the code:
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
{
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.
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.
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.
Activity
itg-assistant commentedon Jun 12, 2026
Frank (ITG) answered 2016-11-13 16:08:40 UTC
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
itg-assistant commentedon Jun 12, 2026
Gunter answered 2016-11-14 09:27:07 UTC
Hi Frank
Thanks for the code. I missed grid.PointToClient(Control.MousePosition).Y,
There are 2 bugs in the code:
private DataGridViewRow GetDragRow(DataGridView grid)
itg-assistant commentedon Jun 12, 2026
Luca answered 2016-11-14 16:37:55 UTC
Hi Gunter,
We added DataGridView.HitTest() to the current build, coming online later on today.
Best,
Luca
itg-assistant commentedon Jun 12, 2026
Mark Reed answered 2017-03-23 01:39:40 UTC
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.
itg-assistant commentedon Jun 12, 2026
Luca answered 2017-03-23 18:48:27 UTC
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
itg-assistant commentedon Jun 12, 2026
Frank (ITG) answered 2017-03-24 00:08:31 UTC
Hi Mark,
the bug was logged as WJ-8140 is fixed in the latest release (1.3.44):
Best regards
Frank
itg-assistant commentedon Jun 12, 2026
Mark Reed answered 2017-03-24 04:00:58 UTC
It is working now thanks :)