Skip to content

Datagridview dragDrop move rows #379

Closed

Description

@itg-assistant

Gunter asked 2016-11-12 20:31:21 UTC

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?

Activity

  1. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    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

  2. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    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:

    • 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.
    
  3. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    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

  4. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    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.

  5. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    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

  6. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    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

  7. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Mark Reed answered 2017-03-24 04:00:58 UTC

    It is working now thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions