拖放在c#中不起作用

本文关键字:不起作用 拖放 | 更新日期: 2023-09-27 18:07:16

我弄清楚了如何在datagridviews之间拖动行,从如何将gridview行从一个网格拖到另一个网格,但现在我有一个问题。我可以拖动行从gridPODetails到DataGridView1。我可以将行从DataGridView1拖回到gridPODetails。但在那之后,我什么也得不到。我希望能够无限地来回拖动,但我只能来回移动。造成这种情况的原因是什么?如何纠正?

 private void gridPODetails_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo info = gridPODetails.HitTest(e.X, e.Y);
            if (info.RowIndex >= 0)
            {
                DataRow view = ((DataTable)(gridPODetails.DataSource)).Rows[info.RowIndex];
                if (view != null)
                {
                    gridPODetails.DoDragDrop(view, DragDropEffects.Copy);
                }
            }
        }
        private void gridPODetails_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        private void gridPODetails_DragDrop(object sender, DragEventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            DataTable table = grid.DataSource as DataTable;
            DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
            if (row != null && table != null && row.Table != table)
            {
                table.ImportRow(row);
                row.Delete();
            }
        }
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
            if (info.RowIndex >= 0)
            {
                DataRow view = ((DataTable)(dataGridView1.DataSource)).Rows[info.RowIndex];
                if (view != null)
                {
                    dataGridView1.DoDragDrop(view, DragDropEffects.Copy);
                }
            }
        }
        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            DataTable table = grid.DataSource as DataTable;
            DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
            if (row != null && table != null && row.Table != table)
            {
                table.ImportRow(row);
                row.Delete();
            }
        }

拖放在c#中不起作用

row.Delete()之后添加table.AcceptChanges()应该允许您在表之间来回移动行。

这样做的原因可能是因为导入先前已删除的行可能会导致问题。

为了回应MAW74656在这个问题下的评论,我已经把我将使用的方法放在一起,如果我要连接多个网格来进行拖放。

本质上,我尝试创建一个lambda来将所有这些功能分组在一个方法中-如果需要的话,它可以作为它自己的方法来完成,尽管允许多个调用者。

在这里:

Func<DataGridView, IEnumerable<Action>> configureDragDrop = grid =>
{
    var dataTable = grid.DataSource as DataTable;
    /* Event handler definitions here - see below */
    grid.MouseDown += mds;
    grid.DragEnter += des;
    grid.DragDrop += dds;
    return new Action[]
    {
        () => grid.MouseDown -= mds,
        () => grid.DragEnter -= des,
        () => grid.DragDrop -= dds,
    };
};

这个代码允许我这样写:

        // form-level field
        private List<Action> removeHandlers = new List<Action>();
        // in the method where `configureDragDrop` is defined
        removeHandlers.AddRange(configureDragDrop(gridPODetails));
        removeHandlers.AddRange(configureDragDrop(dataGridView1));
        removeHandlers.AddRange(configureDragDrop(dataGridView2));
        removeHandlers.AddRange(configureDragDrop(dataGridView3));
        removeHandlers.AddRange(configureDragDrop(dataGridView4));
        removeHandlers.AddRange(configureDragDrop(dataGridView5));
        removeHandlers.AddRange(configureDragDrop(dataGridView6));
        removeHandlers.AddRange(configureDragDrop(dataGridView7));
        // etc

当我关闭窗体时,我可以在一行中删除所有处理程序:

        Array.ForEach(removeHandlers.ToArray(), rh => rh.Invoke());

事件处理程序看起来与原始代码非常相似-只是现在是lambda形式。

MouseDown:

            MouseEventHandler mds = (smd, emd) =>
            {
                var info = grid.HitTest(emd.X, emd.Y);
                if (info.RowIndex >= 0)
                {
                    var dr = dataTable.Rows[info.RowIndex];
                    if (dr != null)
                    {
                        grid.DoDragDrop(dr, DragDropEffects.Copy);
                    }
                }
            };

DragEnter:

            DragEventHandler des = (sde, ede) =>
            {
                ede.Effect = DragDropEffects.Copy;
            };

DragDrop:

            DragEventHandler dds = (sdd, edd) =>
            {
                var dr = edd.Data.GetData(typeof(DataRow)) as DataRow;
                if (dr != null)
                {
                    var dst = dataTable;
                    var src = dr.Table;
                    if (dst != src)
                    {
                        dst.ImportRow(dr);
                        dr.Delete();
                        src.AcceptChanges();
                        dst.AcceptChanges();
                    }
                }
            };