Hallo!
"HitTestInfo" liefert alle erforderlichen Informationen.
Hier ein einfaches Beispiel, bei dem nur die Werte aus
Integer-Zellen im Ziel auf Integer-Zellen und die Werte aus
Double-Zellen im Ziel auf Double-Zellen abgelegt werden können.
Die Funktion CheckDestinationCell kannst Du für Deine Zwecke
anpassen.
Public Class frmDGVDragDrop
Dim WithEvents dgvSource As New DataGridView With
{.Parent = Me, .Location = New Point(10, 10), .Size = New Drawing.Size( _
300, 300)}
Dim dtsource As New DataTable
Dim WithEvents dgvDestination As New DataGridView With
{.Parent = Me, .Location = New Point(310, 10), .Size = New Drawing.Size( _
300, 300)}
Dim dtDestination As New DataTable
Dim Source_ClickedCell As DataGridViewCell
Private Sub frmDGVDragDrop_Load(sender As Object, e As EventArgs) Handles _
MyBase.Load
Me.Size = New Size(630, 400)
With dtSource
.Columns.Add("AAA", GetType(Integer))
.Columns.Add("BBB", GetType(Double))
.Rows.Add(1, 123.45)
.Rows.Add(2, 678.9)
End With
With dtDestination
.Columns.Add("XXX", GetType(Double))
.Columns.Add("YYY", GetType(Integer))
.Rows.Add(1, 54.321)
.Rows.Add(2, 9.876)
End With
dgvSource.DataSource = dtsource
dgvDestination.DataSource = dtDestination
dgvDestination.AllowDrop = True
End Sub
Private Sub dgvSource_MouseMove(sender As Object, e As MouseEventArgs) _
Handles dgvSource.MouseMove
If (e.Button And MouseButtons.Right) = MouseButtons.Right Then
Dim dropeffect As DragDropEffects =
dgvSource.DoDragDrop(Source_ClickedCell, DragDropEffects.Copy)
End If
End Sub
Private Sub dgvSource_MouseDown(sender As Object, e As MouseEventArgs) _
Handles dgvSource.MouseDown
If e.Button = MouseButtons.Right Then
Dim hti As DataGridView.HitTestInfo = dgvSource.HitTest(e.X, e.Y)
If hti.Type = DataGridViewHitTestType.Cell Then
Source_ClickedCell = dgvSource.Rows(hti.RowIndex).Cells( _
hti.ColumnIndex)
Else
Source_ClickedCell = Nothing
End If
End If
End Sub
Private Sub dgvSource_MouseUp(sender As Object, e As MouseEventArgs) _
Handles dgvSource.MouseUp
Source_ClickedCell = Nothing
End Sub
Private Sub dgvDestination_DragOver(sender As Object, e As DragEventArgs) _
Handles dgvDestination.DragOver
If CheckDestinationCell(e) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub dgvDestination_DragDrop(sender As Object, e As DragEventArgs) _
Handles dgvDestination.DragDrop
Dim destinationcell As DataGridViewCell
If CheckDestinationCell(e, destinationcell) Then
destinationcell.Value = Source_ClickedCell.Value
End If
End Sub
Private Function CheckDestinationCell(e As DragEventArgs,
Optional ByRef DestinationCell As DataGridViewCell = Nothing) As Boolean
DestinationCell = Nothing
If Not e.Data.GetDataPresent(GetType(DataGridViewTextBoxCell)) Then
Return False
ElseIf (e.AllowedEffect And DragDropEffects.Copy) = _
DragDropEffects.Copy Then
Dim c As Point = dgvDestination.PointToClient(New Point(e.X, e.Y))
Dim hti As DataGridView.HitTestInfo = dgvDestination.HitTest(c.X, _
c.Y)
If hti.Type = DataGridViewHitTestType.Cell Then
Dim TestCell As DataGridViewCell =
dgvDestination.Rows(hti.RowIndex).Cells(hti.ColumnIndex)
If TestCell.ValueType = Source_ClickedCell.ValueType AndAlso
hti.RowIndex = dgvDestination.Rows.Count - 1 Then
Me.Text = hti.RowIndex.ToString
DestinationCell = TestCell : Return True
End If
End If
End If
Return False
End Function
End Class |