vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Blitzschnelles Erstellen von grafischen Diagrammen!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2024
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

VB.NET - Ein- und Umsteiger
Re: DragDrop im DGV auf Zeilenkenner für neue Zeile 
Autor: Manfred X
Datum: 04.04.21 15:14

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
alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
DragDrop im DGV auf Zeilenkenner für neue Zeile465Bazi04.04.21 10:11
Re: DragDrop im DGV auf Zeilenkenner für neue Zeile245Bazi04.04.21 10:38
Re: DragDrop im DGV auf Zeilenkenner für neue Zeile248Manfred X04.04.21 15:14
Re: DragDrop im DGV auf Zeilenkenner für neue Zeile247Bazi04.04.21 21:50
Re: DragDrop im DGV auf Zeilenkenner für neue Zeile238Manfred X05.04.21 11:22

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2024 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel