Drag And Drop to a specific datagridview cell

HeavenCore

Well-known member
Joined
Apr 10, 2007
Messages
77
Location
Bolton, England
Programming Experience
1-3
HI, i have a collection of panels with different background colors, i need to make it so when a panel is dragged onto the grid the cell its dropped on changes to the same colour as the background of the dragged panel. I have the following code on my panel:

VB.NET:
    Private Sub Panel3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseDown
        StartDragTask = 8
        Panel3.DoDragDrop(Panel3.BackColor, DragDropEffects.Copy)
    End Sub

but when it comes to the dragEnter and dragDrop on datagrid view i dont know where to start, Any help would be great!

Regards

HC
 
ok, its kinda complicating but i have nearly got it:

VB.NET:
    Private Sub Panel3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseDown
        StartDragTask = 8
        Panel3.DoDragDrop(Panel3.BackColor.ToString, DragDropEffects.Copy)
    End Sub

    Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
        If e.Data.GetDataPresent(GetType(System.String)) Then
            Dim str As String = CType(e.Data.GetData(GetType(System.String)), System.String)
            Dim dgv As DataGridView = DirectCast(sender, DataGridView)
            Dim p As Point = dgv.PointToClient(New Point(e.X, e.Y))
            Dim hti As DataGridView.HitTestInfo = dgv.HitTest(p.X, p.Y)

            If hti.Type = DataGridViewHitTestType.Cell Then
                Dim cell As DataGridViewCell = dgv(hti.ColumnIndex, hti.RowIndex)
                Dim colour As System.Drawing.Color
                colour = System.Drawing.Color.FromName(str)
                cell.Style.BackColor = colour
            End If
        End If

    End Sub

    Private Sub DataGridView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver
        Dim dgv As DataGridView = CType(sender, DataGridView)
        Dim p As Point = dgv.PointToClient(New Point(e.X, e.Y))
        Dim hti As DataGridView.HitTestInfo = dgv.HitTest(p.X, p.Y)

        If hti.Type = DataGridViewHitTestType.Cell Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If

    End Sub

only problem is the cell does not change colour :S when debugging the color is applied to the cell and i i select the cell and un-select it it goes gray (system default selection color i think) does the cell need re-drawing or something?
 
Back
Top