Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 808
  • Last Modified:

Hit Test returns -1 coordinates VB.net datagridview

Hi

The code bellow returns -1 on both row and col index

any ideas why ? The X and Y coordinates show positive numbers
Dim hitTest As DataGridView.HitTestInfo
 
        hitTest = Me.DataGridView13.HitTest(e.X, e.Y)
 
        If hitTest.RowIndex <> -1 AndAlso hitTest.ColumnIndex <> -1 Then
            Dim cell As DataGridViewCell = Me.DataGridView13(hitTest.ColumnIndex, hitTest.RowIndex)
            cell.Value = e.Data.GetData(DataFormats.Text)
        End If

Open in new window

0
MichMat
Asked:
MichMat
  • 4
  • 3
1 Solution
 
jpaulinoCommented:
What you want to do and in what event are you using that code ?
0
 
MichMatAuthor Commented:
Im trying to draging and dropping item from a listbox into a datagridview cell
 Private Sub DataGridView13_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView13.DragDrop
 
 
 
        Dim hitTest As DataGridView.HitTestInfo
 
        hitTest = Me.DataGridView13.HitTest(e.X, e.Y)
 
        If hitTest.RowIndex <> -1 AndAlso hitTest.ColumnIndex <> -1 Then
            Dim cell As DataGridViewCell = Me.DataGridView13(hitTest.ColumnIndex, hitTest.RowIndex)
            cell.Value = e.Data.GetData(DataFormats.Text)
        End If
 
       
 
 
 
    End Sub

Open in new window

0
 
jpaulinoCommented:
Sorry e.Data.GetData should be for DragDrop:)
The e.X gives you the point on the screen and not in the datagridview. You cannot use that way and you have to first know where that point belong in the grid. Then you use the HitTestInfo.
 Try this way:

    Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
 
        Dim p As Point = Me.DataGridView1.PointToClient(New Point(e.X, e.Y))
        Dim hitTest As DataGridView.HitTestInfo = DataGridView1.HitTest(p.X, p.Y)
 
        If hitTest.RowIndex <> -1 AndAlso hitTest.ColumnIndex <> -1 Then
            Dim cell As DataGridViewCell = Me.DataGridView1(hitTest.ColumnIndex, hitTest.RowIndex)
            cell.Value = e.Data.GetData(DataFormats.Text)
        End If
 
 
    End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MichMatAuthor Commented:
Im getting an error when running your code, its comes up as a Mesage Box and says that my string isnt recognised as a valid bolleon Im including all the code that Im using to do this drag and drop may be you could spot the error thank you
the only otherthing is that the datagid has dragdrop enabeled
Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim index As Integer = ListBox1.SelectedIndex
            If index <> -1 Then
                LBvalue = ListBox1.Items(index)
                ListBox1.DoDragDrop(LBvalue, DragDropEffects.All)
            End If
        End If
    End Sub
 
    Private Sub DataGridView13_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView13.DragDrop
 
 
        
        Dim p As Point = Me.DataGridView13.PointToClient(New Point(e.X, e.Y))
        Dim hitTest As DataGridView.HitTestInfo = DataGridView13.HitTest(p.X, p.Y)
 
        If hitTest.RowIndex <> -1 AndAlso hitTest.ColumnIndex <> -1 Then
            Dim cell As DataGridViewCell = Me.DataGridView13(hitTest.ColumnIndex, hitTest.RowIndex)
            cell.Value = e.Data.GetData(DataFormats.Text)
        End If
 
    End Sub
 
 
    Private Sub DataGridView13_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView13.DragEnter
        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

Open in new window

0
 
MichMatAuthor Commented:
Just figgured it out I had the grid column formated at checkbox type ??????

Your code now works perfectly
0
 
MichMatAuthor Commented:
As always A1 work, Im always happy when JP takes my questions as I know I will get a great result. Thank You
0
 
jpaulinoCommented:
Glad I could help MichMat!
jpaulino
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now