Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target a datagridview row or cell from DragDrop event?

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?

like image 861
Dave Avatar asked Dec 14 '22 01:12

Dave


1 Answers

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

like image 180
Michael G Avatar answered Feb 14 '23 07:02

Michael G