Windows > Windows Forms Forums > Windows Forms Data Controls and Databinding > Drag and drop from a datagridview to another
Ask a questionAsk a question
 

AnswerDrag and drop from a datagridview to another

  • Monday, February 20, 2006 6:01 PMbalves Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello,

    I'm having problems with drag and drop of a cell value (copy) from one datagridview to another, because id drops two cells below of the point I release the mouse. I calculated the difference of the hittest "y" coordinate and it's 50 pixels. The strangest thing is, that difference increases exponentially if the form is not maximized.

    The purpose of the code below, is to copy the value of a cell in OriginGrid to a specific cell on DestinationGrid. I belive the problem has to do with the "hittest" being relative but I can't figure out why. Any ideas?

    Private Sub OriginGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles OriginGrid.MouseDown

    Dim hit As DataGridView.HitTestInfo = OriginGrid.HitTest(e.X, e.Y)

    If hit.Type = DataGridViewHitTestType.Cell Then

    clickedCell = OriginGrid.Rows(hit.RowIndex).Cells(hit.ColumnIndex)

    OriginGrid.DoDragDrop(clickedCell.Value, DragDropEffects.Copy)

    End If

    End Sub

    Private Sub DestinationGrid_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DestinationGrid.DragDrop

    Dim hit As DataGridView.HitTestInfo = DestinationGrid.HitTest(e.X, e.Y)

    If hit.Type = DataGridViewHitTestType.Cell Then

    clickedCell = DestinationGrid.Rows(hit.RowIndex).Cells(hit.ColumnIndex) 'rowindex is two cells below on a maximized form...

    clickedCell.Value = e.Data.GetData(DataFormats.Text).ToString

    End If

    End Sub

    Private Sub DestinationGrid_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DestinationGrid.DragEnter

    If (e.Data.GetDataPresent(DataFormats.Text)) Then

    e.Effect = DragDropEffects.Copy

    Else

    e.Effect = DragDropEffects.None

    End If

    End Sub

Answers

  • Thursday, February 23, 2006 2:53 PMbalves Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
     

    Ok, I found the answer: the position is indeed relative so instead of

    Dim hit As DataGridView.HitTestInfo = DestinationGrid.HitTest(e.X, e.Y)

    I had to write something like:

    Dim hit As DataGridView.HitTestInfo = DestinationGrid.HitTest(MousePosition.X - Me.Location.X - 3, MousePosition.Y - 54 - Me.Location.Y)

    These parameters are for a regular form with a menubar on top (54).

All Replies

  • Tuesday, February 21, 2006 11:13 AMbalves Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sorry,

    I forgot the following line on top of the module:

    Private clickedCell As DataGridViewCell

  • Thursday, February 23, 2006 2:53 PMbalves Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
     

    Ok, I found the answer: the position is indeed relative so instead of

    Dim hit As DataGridView.HitTestInfo = DestinationGrid.HitTest(e.X, e.Y)

    I had to write something like:

    Dim hit As DataGridView.HitTestInfo = DestinationGrid.HitTest(MousePosition.X - Me.Location.X - 3, MousePosition.Y - 54 - Me.Location.Y)

    These parameters are for a regular form with a menubar on top (54).

  • Monday, March 13, 2006 3:13 AMdave_sky Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi balves,

    Could you tell me how your datagridview columns were set up?

    Thanks,

    dave_sky

  • Monday, March 27, 2006 6:12 PMitsFF Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What you could also do is to translate screen coordinates to client coordinates. It would look something like this:

    Point clientPoint = DestinationGrid.PointToClient(new Point(e.X, e.Y));
    DataGridView.HitTestInfo hit = DestinationGrid.HitTest(clientPoint.X, clientPoint.Y);


    Hope that helps.
  • Friday, January 18, 2008 9:54 PMThomasBHolland Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals