【问题标题】:Winforms datagridview : drag and drop causes an errorWinforms datagridview:拖放导致错误
【发布时间】:2015-07-10 11:49:25
【问题描述】:

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

每当我尝试拖放时,拖放都是可以的,但拖放会导致我上面提到的错误,如果可能的话,我不想使用绑定列表,因为如果我使用它,我将不得不进行大量更改。 如果您能提供帮助,我将不胜感激。

【问题讨论】:

  • 如果要双向绑定,则需要使用绑定列表。
  • 我不知道该怎么做。这不起作用IEnumerable&lt;EFESRDP0&gt; query = from p in db.EFESRDP0s select p; testList = query.ToList(); var list = new BindingList&lt;EFESRDP0&gt;(testList); dataGridView1.DataSource = list;
  • 据我所知,虽然您可以尝试做一些研究以寻找替代方案。
  • 您的 bindingList 需要位于 BindingSource 中,如下所示:var bindingSource = new BindingSource(); bindingSource.DataSource = (from p in db.myTables select p).ToBindingList(); dgv1.DataSource = bindingSource;
  • 我正在使用 linq to sql,但您的提议需要 EF。我不能应用它,因为如果我这样做,我将不得不对我的整个项目进行更改。 @SpaceSteak

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2013-04-21
    • 2015-01-27
    相关资源
    最近更新 更多