Solved

Hit Test returns -1 coordinates VB.net datagridview

Posted on 2009-04-12
7
791 Views
Last Modified: 2012-05-06
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
Comment
Question by:MichMat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24124911
What you want to do and in what event are you using that code ?
0
 

Author Comment

by:MichMat
ID: 24124916
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
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24124941
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:MichMat
ID: 24125287
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
 

Author Comment

by:MichMat
ID: 24125303
Just figgured it out I had the grid column formated at checkbox type ??????

Your code now works perfectly
0
 

Author Closing Comment

by:MichMat
ID: 31569289
As always A1 work, Im always happy when JP takes my questions as I know I will get a great result. Thank You
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24125817
Glad I could help MichMat!
jpaulino
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question