返回 DataGridView 中拖放放置的单元格

本文关键字:单元格 拖放 DataGridView 返回 | 更新日期: 2023-09-27 18:36:20

我有一个用C#编写的Winforms应用程序。

我可以在同一窗体上成功地将值从一个数据网格视图拖放到另一个数据网格视图。

但是,我不确定如何确定接收 DataGridView 中的哪个单元格拖放操作终止。

我尝试了以下代码 -

private void dataContactBusiness_DragDrop(object sender, DragEventArgs e)
{
    var cell = dataContactBusiness.HitTest(e.X, e.Y);
    //...other operations continue here    
}

但是我的命中测试总是越界,即返回 -1 的行和列索引。

返回 DataGridView 中拖放放置的单元格

我使用了以下代码,它有效

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

根据o_weisman评论