Invalid coordinates from dataGridView HitTest

P

Parrot

I am trying to implement a drag and drop operation from a listbox to a data
gridview control but I keep getting -1 for my row index when the dragdrop
function is executed. Below is my coding.

DataGridView.HitTestInfo info = dataGridView2.HitTest(e.X, e.Y);
int row = info.RowIndex;

The coordinates returned to the dragdrop function seem to be coordinates
relative to the window rather than to the datagridview control. In any case,
I get -1 for of the indices returned in the info group. Does anyone know how
to get the correct row in a dragdrop into a datagridview.
Dave
 
S

Stefano Tonello

Parrot ha scritto:
I am trying to implement a drag and drop operation from a listbox to a data
gridview control but I keep getting -1 for my row index when the dragdrop
function is executed. Below is my coding.

DataGridView.HitTestInfo info = dataGridView2.HitTest(e.X, e.Y);
int row = info.RowIndex;

The coordinates returned to the dragdrop function seem to be coordinates
relative to the window rather than to the datagridview control. In any case,
I get -1 for of the indices returned in the info group. Does anyone know how
to get the correct row in a dragdrop into a datagridview.
Dave

Hi Parrot,
you have to use
Control.PointToClient(pt)

Bests,
Stefano
 
P

Parrot

Stefano:
Thanks for your reply. I inserted the following command before the HitTest
as shown below and the row index was correct.

Point p = dataGridView2.PointToClient(new Point(e.X, e.Y);
DataGridView.HitTestInfo info = dataGridView2.HitTest(p.X, p.Y);
int row = info.RowIndex;

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top