1

同じフォームの 2 つのデータグリッドビュー間でドラッグ アンド ドロップを実装しようとしています。SQL Server ビューにデータソースがバインドされた DataGridView1 と DataGridView2 があります。および次のコード:

 Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        Dim Index As Integer
        Index = DataGridView1.HitTest(e.X, e.Y).RowIndex
        If Index > -1 Then
            'Pass the Index as "Data" argument of the DoDragDrop Function
            Me.DataGridView1.Rows(Index).Selected = True
            DataGridView1.DoDragDrop(Index, DragDropEffects.Move)
        End If
 End Sub

Private Sub DataGridView2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
    Dim Index As Integer
    Index = DataGridView2.HitTest(e.X, e.Y).RowIndex
    If Index > -1 Then
        'Pass the Index as "Data" argument of the DoDragDrop Function
        Me.DataGridView2.Rows(Index).Selected = True
        DataGridView1.DoDragDrop(Index, DragDropEffects.Move)
    End If
End Sub

Private Sub DataGridView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
    Try
        Dim myID As Integer = Convert.ToInt32(e.Data.GetData(Type.GetType("System.Int32")))

        (...
         code to execute query to add selected value to a table
         ...) 

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub DataGridView2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop
    Try
        Dim myID As Integer = Convert.ToInt32(e.Data.GetData(Type.GetType("System.Int32")))

        (...
         code to execute query to delete selected value from a table
         ...)    
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

すべてがほぼ計画どおりに機能します。
最初のデータグリッドからレコードを移動し、2 番目のデータグリッドにドラッグ アンド ドロップすると、すべてのコードが正常に機能し、レコードは SQL を介して 2 番目のデータグリッドビューに移動しますが、2 番目のデータグリッドビューをクリックすると、 、ドラッグ イベントが再び発生し、2 番目のデータグリッドから最初のデータグリッドにドラッグするようなものです。この動作を防ぐにはどうすればよいですか。

4

1 に答える 1