c# - 在 DataGridView 之间拖放

标签 c# winforms datagridview drag-and-drop

我复制了一些代码并对其进行了修改以适合我的应用程序。我将继续调整和清理代码,直到我满意为止。但我遇到了一个小错误。我有两个数据 GridView ,希望将数据网格行从一个移动到另一个。但是,虽然 drag&drop 事件全部触发,但 dataGridView_Routes_DragDrop() 将执行日志命令,因为 e.Data.GetData 中没有数据。我做错了什么?我错过了什么吗?我尝试浏览几份指南,但没有任何内容具体涵盖此问题。

如何让数据网格将拖动的数据网格行传递到另一个数据网格?

    /* Drag & Drop */
    private Rectangle dragBoxFromMouseDown;
    private int rowIndexFromMouseDown;
    private void dataGridView_Trips_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // If the mouse moves outside the rectangle, start the drag.
            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {
                // Proceed with the drag and drop, passing in the list item.                    
                DragDropEffects dropEffect = dataGridView_Trips.DoDragDrop(dataGridView_Trips.Rows[rowIndexFromMouseDown], DragDropEffects.Copy);
            }
        }
    }

    private void dataGridView_Trips_MouseDown(object sender, MouseEventArgs e)
    {
        // Get the index of the item the mouse is below.
        rowIndexFromMouseDown = dataGridView_Trips.HitTest(e.X, e.Y).RowIndex;
        if (rowIndexFromMouseDown != -1)
        {
            // Remember the point where the mouse down occurred. 
            // The DragSize indicates the size that the mouse can move 
            // before a drag event should be started.                
            Size dragSize = SystemInformation.DragSize;

            // Create a rectangle using the DragSize, with the mouse position being
            // at the center of the rectangle.
            dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
        }
        else
            // Reset the rectangle if the mouse is not over an item in the ListBox.
            dragBoxFromMouseDown = Rectangle.Empty;
    }

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

    private void dataGridView_Routes_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(DataRowView)))
        {
            // The mouse locations are relative to the screen, so they must be 
            // converted to client coordinates.
            Point clientPoint = dataGridView_Routes.PointToClient(new Point(e.X, e.Y));

            // If the drag operation was a copy then add the row to the other control.
            if (e.Effect == DragDropEffects.Copy)
            {
                DataGridViewRow rowToMove = e.Data(typeof(DataGridViewRow)) as DataGridViewRow;
                dataGridView_Routes.Rows.Add(rowToMove);
            }
        }
        else
        {
            log("Geen data! #01", "Fout");
        }
    }
    /* End Drag & Drop */

最佳答案

我不知道。但以下功能已经过调整并且可以按预期工作。不太确定以前的代码是如何损坏的。

编辑: typeof 是用 DataViewRow 而不是 DataGridViewRow 编写的。失败。

private void dataGridView_Routes_DragDrop(object sender, DragEventArgs e)
    {
        try
        {
            if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {

                // The mouse locations are relative to the screen, so they must be 
                // converted to client coordinates.
                Point clientPoint = dataGridView_Routes.PointToClient(new Point(e.X, e.Y));

                // If the drag operation was a copy then add the row to the other control.
                if (e.Effect == DragDropEffects.Copy)
                {
                    DataGridViewRow Row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                    dataGridView_Routes.Rows.Add(Row.Cells[0].Value, Row.Cells[1].Value, Row.Cells[2].Value);
                }
            }
            else
            {
                log("Geen data! #01", "Fout");
            }
        }
        catch (Exception msg)
        {
            log(msg.Message,"Fout");
        }
    }

关于c# - 在 DataGridView 之间拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199510/

相关文章:

c# - 传入和传出后台 worker 的数据会发生什么变化?

c# - 在哪里放置临时的非管理文件?

c# - WindowsMediaPlayer (WMPLib) 音频突然消失

winforms - DataGridView行: Semi-transparent selection or row border on selection

c# - 在 finally block 中尝试

c# - 错误 CS2012 : Cannot open <executable path> access to <executable path denied>

.net - .NET 桌面应用程序的好例子

winforms - WM_NCHITTEST、HTCAPTION 和最大化窗口

c# - datagridView编辑

c# - 如何在 DataGridViewCell 中托管控件以进行显示和编辑?