我想出了如何从how to drag gridview row from one grid to another拖动datagridviews之间的行,但是现在我遇到了问题。我可以将行从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();
            }
        }

最佳答案

table.AcceptChanges()之后添加row.Delete()应该使您可以在表之间向后移动行。

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

关于c# - 拖放在C#中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7247641/

10-10 22:09