views:

464

answers:

2

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
+1  A: 

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.

gbianchi
A: 

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

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