DataGridView, Drag/Drop, Sorting RRS feed

  • Question

  • I've implemented Drap/Drop into a dataset-bound DataGridView that seems to be working well.  On MouseDown, the cursor changes to the copy cursor and I can drag a row to a new location.  However, if I then sort the data by clicking on one of the column headers, the data sorts properly but Drag/Drop no longer works. On the first drag after the sort, adjacent rows are highlighted.  Clicking on a row again restores the copy cursor on drag but drag/drop does not happen.  Apparently the DGVis in a different state after automatic sorting than before.

    I'd appreciate any pointers on how to troubleshoot this issue.


    Thursday, December 30, 2010 1:21 PM

All replies

  • Hello JohnZonie,

    Would you post a sample code that can reproduce the problem? It's hard to figure out the root cause just from symptom you discribed.

    You may also post image if needed. For inserting the graphics in forum, you can put your image in skydrive, and add the following HTML in HTML Source Editor:
    <img src="(the URL of the graphics)">

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 31, 2010 2:19 AM
  • Hi Helen,

    Thanks for the reply.  I am using drag/drop code as outlined here:


    Apply this code to any DGV with sorting enabled.  Before sorting, rows can be dragged and dropped successfully.  Applying a single sort by clicking on a column header seems to prevent the drag/drop operation.

    I guess one way to "reset" the DGV after sorting would be to remove it from the container and recreate it but that seems very painful.



    Friday, December 31, 2010 4:47 AM
  • Hi,

    I am still struggling with this issue.  To reiterate, I have a DGV bound to a dataset.  The following code allows drag/drop to function properly to rearrange data rows in the DGV.  However, if I use the sort function by clicking on a column header, the data sorts properly but darg/drop no longer functions.  Here is the drag/drop code fragment.

        Dim SourceRowIndex As Integer = -1
        Dim SourceColumnIndex As Integer = -1

      Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
            SourceRowIndex = -1
            If e.Button = Windows.Forms.MouseButtons.Left And TargetRowSelected >= 0 Then
                SourceRowIndex = DataGridView1.HitTest(e.X, e.Y).RowIndex
                SourceColumnIndex = DataGridView1.HitTest(e.X, e.Y).ColumnIndex

                'allow drag/drop only when clicking on the row header
                If SourceColumnIndex = -1 And SourceRowIndex >= 0 Then
                    DataGridView1.Rows(SourceRowIndex).Selected = True
                    DoDragDrop(DataGridView1.Rows(SourceRowIndex), DragDropEffects.Move)
                End If
            End If
        End Sub

      Private Sub DataGridView1_MouseUp(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp

            SourceRowIndex = -1
            SourceColumnIndex = -1
        End Sub

      Private Sub DataGridView1_DragDrop(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop

            Dim pt As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
            Dim TargetRowIndex As Integer = DataGridView1.HitTest(pt.X, pt.Y).RowIndex
            If TargetRowIndex > SourceRowIndex Then
                TargetRowIndex -= 1
            End If
            Dim drMove As DataRow = DataSet1.Tables("Data").NewRow
            drMove.ItemArray = DataSet1.Tables("Data").Rows(SourceRowIndex).ItemArray
            If TargetRowIndex >= 0 Then
                DataSet1.Tables("Data").Rows.InsertAt(drMove, TargetRowIndex)
            End If
        End Sub

      Private Sub DataGridView1_DragOver(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver

            If SourceRowIndex >= 0 And SourceColumnIndex = -1 Then
                e.Effect = DragDropEffects.Move
            End If
        End Sub

    It appears the "state" of the DGV changes after a sort as evidenced by the up/down sort glyph.  As long as the glyph is not visible, drag/drop works fine.  The only way I have found to restore the state to "drag/drop-able" is to restart the program, which of course reinitializes the DGV.

    Any thoughts or troubleshooting ideas would be greatly appreciated.


    Sunday, January 9, 2011 12:06 PM