如何从DragDrop事件定位datagridview行或单元格? [英] How to target a datagridview row or cell from DragDrop event?

查看:441
本文介绍了如何从DragDrop事件定位datagridview行或单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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("!");
        }

    }

如果我尝试拖动一个项目到具有上述测试代码的datagridview,我从 data.ToString() ok中收到正确的数据,但是我无法定位一行或者单元格。

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.

RowIndex.ToString()返回-1,我的if语句返回false,所以从不进入编码块。

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

我的代码有什么问题?

推荐答案

我相信坐标正在传递来自DragAndDrop是屏幕空间坐标。
我认为你需要把点分配给客户端坐标空间。

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);

然后 hitTest.Row 将有行索引

这篇关于如何从DragDrop事件定位datagridview行或单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆