Monday, April 11, 2011

How to I prevent 'self' drag and drop in a datagridview?

I'm implementing drag and drop functionality between two datagridviews. This works as intended with one exception: it is possible to drag and drop within the same datagridview. This results in duplicated rows. I want to limit the functionality so that I can only drag from one datagridview to another. Does anyone know how this can be achieved? I'm guessing some kind of hit test is required but I'm not sure how to implement this...

The code I am using is as follows:

Private Sub dgvFMAvailable_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvFMAvailable.MouseMove

    If e.Button = Windows.Forms.MouseButtons.Left Then
        Me.dgvFMAvailable.DoDragDrop(Me.dgvFMAvailable.SelectedRows, DragDropEffects.Move)
    End If

End Sub

and

Private Sub dgvFMSelected_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgvFMSelected.DragDrop

    Try
        Me.SelectFM(CType(e.Data.GetData(GetType(DataGridViewSelectedRowCollection)), DataGridViewSelectedRowCollection))

    Finally
        e.Effect = DragDropEffects.None
    End Try

End Sub
From stackoverflow
  • Just a quick idea. What if when you start the drag you hold the name of the origin grid. When you do the drop check the name, if they are the same object then don't allow the drop.

  • Simply test for reference equality when dropping. Something like this:

    If Object.ReferenceEquals(droppedThing, thingWhereItWasDropped)
        ' Don't drop it
    Else
        ' Drop it
    End If
    
    Simon : How do I know where the thing that I am dropping comes from?

0 comments:

Post a Comment