views:

225

answers:

1
void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        object data = e.Data.GetData(typeof(string));

        MessageBox.Show(e.X + " " + e.Y + " " + dataGridView1.HitTest(e.X, e.Y).RowIndex.ToString());

        if (dataGridView1.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.Cell)
        {
            MessageBox.Show("!");
        }

    }

If I try to drag an item to a datagridview with the above test code, I receive the correct data from the data.ToString() ok but i cannot target a row or cell.

The RowIndex.ToString() returns "-1", and my if statement returns false so never enters if coded block.

What is wrong with my code?

A: 

I believe the coordinates being passed from DragAndDrop are screen space coordinates. I think you need to cast the points to the client coordinates space.

Point dscreen = new Point(e.X, e.Y);
Point dclient = dataGridView1.PointToClient(dscreen );
DataGridView.HitTestInfo hitTest = dataGridView1.HitTest(dclient.X,dclient.Y);

then hitTest.Row will have the row index

Michael G
worked a treat! I will look up what I did with the PointToClient etc thanks
Dave