在多个表单上的两个网格视图之间拖放

Drag Drop Between two Grid Views on multiple forms

所以我有一个主窗体,然后是多个 windows,其中包含数据网格视图。

我希望能够在主 window 和 child windows 之间交换数据,反之亦然。

我的主表格上有:

private Rectangle dragBoxFromMouseDown;
    private object valueFromMouseDown;

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        var hittestInfo = dataGridView1.HitTest(e.X, e.Y);
        if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
        {
            valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex];
            if (valueFromMouseDown != null)
            {
                Size dragSize = SystemInformation.DragSize;
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
            }
        }
        else
            dragBoxFromMouseDown = Rectangle.Empty;
    }

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {
                DragDropEffects dropEffect = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy);
            }
        }
    }

然后在我的 child 表格上:

        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(System.String)))
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }

    private void dataGridView1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
        DataGridView.HitTestInfo hit = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);

        if (hit.RowIndex != -1)
        {
            dataGridView1.Rows.Insert(hit.RowIndex, e.Data.GetData(typeof(Objects.Amazon.PoDetail)));
        }
        else
        {
            dataGridView1.Rows.Add(e.Data.GetData(typeof(Objects.Amazon.PoDetail)));
        }
    }

显然,它在 e.Data.GetData 处失败,因为它以 child 形式从当前数据网格视图获取数据。

我想不出在表单之间传递数据的方法。

这怎么可能。拖放数据与您在 DoDragDrop 方法中传递的类型相同。因此它是 DataGridViewRow 类型。所以应该是-

object dropData = e.Data.GetData(typeof(DataGridViewRow));