Winforms datagridview:拖放会导致错误

时间:2015-07-10 11:49:26

标签: c# winforms datagridview drag-and-drop bindinglist

System.Windows.Forms.dll中发生了'System.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);
        }
    }

每当我试图拖动&amp; drop,拖动是好的,但drop导致我上面提到的错误,如果可能的话我不想使用bindinglist,因为如果我使用它,我将不得不进行大量的更改。 如果你能帮助我会很有帮助。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我对鼠标事件进行了更改

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");
        }
    }