Winforms datagridview:拖放会导致错误

本文关键字:错误 拖放 datagridview Winforms | 更新日期: 2023-09-27 18:02:16

类型为'System '的例外。在System.Windows.Forms.dll中发生了InvalidOperationException,但未在用户代码中处理附加信息:除非DataGridView数据绑定到支持更改通知并允许删除的IBindingList,否则不能以编程方式删除行。

这是我如何绑定我的数据到datagridview:

IEnumerable<myTable> query = from p in db.myTables select p;
        testList = query.ToList();
        dataGridView1.DataSource = testList;

这是我用来拖动&删除行:

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        if (dataGridView1.SelectedRows.Count == 1)
        {
            if (e.Button == MouseButtons.Left)
            {
                rw = dataGridView1.SelectedRows[0];
                rowIndexFromMouseDown = dataGridView1.SelectedRows[0].Index;
                dataGridView1.DoDragDrop(rw, DragDropEffects.Move);
            }
        }
    }        
    private void dataGridView1_DragEnter(object sender, DragEventArgs e)
    {
        if (dataGridView1.SelectedRows.Count > 0)
        {
            e.Effect = DragDropEffects.Move;
        }
    }
    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        int rowIndexOfItemUnderMouseToDrop;
        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
        rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
        if (e.Effect == DragDropEffects.Move)
        {
            dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
            dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rw);
        }
    }

每当我想拖拽&拖放是可以的,但是拖放会导致我上面提到的错误,如果可能的话,我不想使用绑定列表,因为如果我使用它,我将不得不做很多改变。如果你能帮忙,我将不胜感激。

Winforms datagridview:拖放会导致错误

我解决了这个问题。我修改了鼠标事件

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {                
            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {                                       
                DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Copy);
            }
        }
    }
    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        // Get the index of the item the mouse is below.
        rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
        if (rowIndexFromMouseDown != -1)
        {                             
            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_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }
    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        try
        {
            if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));                   
                if (e.Effect == DragDropEffects.Copy)
                {
                    DataGridViewRow Row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                    dataGridView1.Rows.Add(Row.Cells[0].Value, Row.Cells[1].Value, Row.Cells[2].Value);
                }
            }
            else
            {
                //Reflect the exception to screen
                MessageBox.Show("Geen data! #01", "Error");
            }
        }
        catch (Exception msg)
        {
            MessageBox.Show(msg.Message, "Error");
        }
    }