“BindingSource不能是其自己的数据源” - 尝试从另一个类中的方法重置绑定源时出错 [英] "BindingSource cannot be its own data source" - error when trying to reset the binding source from a method in another class

查看:1634
本文介绍了“BindingSource不能是其自己的数据源” - 尝试从另一个类中的方法重置绑定源时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 BindingSource 绑定了一个 DataGridview 。所以在主线程中,我们已经给出了这样的结果。

  class1BindingSource = new BindingSource(); 
class1BindingSource.DataSource = class1List;
this.dataGridView1.DataSource = class1BindingSource;

之后,我在表单中放置了一个后台工作人员,并在按钮中触发。 / p>

ie在按钮中点击

  this.backgroundWorker1.RunWorkerAsync()

BackgroundWorker DoWork事件我正在尝试更新 BindingSource ,并尝试更新 DataGridview



所以 BindingSource 重置是在另一个类的一个方法中完成的。



DoWork事件

  Class2 cl2 = new Class2(); 
cl2.UpdateBindingSource(class1BindingSource);

UpdateBindingSource方法

  public void UpdateBindingSource(BindingSource bs)
{
Class1 c1 = bs.Current as Class1; (int i = 0; i <1000; i ++)
{
lock(bs.SyncRoot)
{
c1.MyProperty1 = i;
bs.ResetItem(0);
}
}
}

现在我得到一个例外像 BindingSource 不能是自己的数据源。不要将 DataSource DataMember 属性设置为返回到 BindingSource



如果我在我的 DoWork Event 中这样做,那么我可以重置控制线程中的项目本身使用 BeginInvoke方法



但实际上我正在尝试模拟我们的应用场景。所以我想以这种格式解决这个问题。



任何人都可以帮助我。

解决方案

问题是您不能在另一个线程内比gui线程更新 BindingSource 。这是因为, BindingSource 将触发一些事件,然后数据网格视图将接收到一些事件,然后它们将自动更新,这将导致它失败不要在gui线程上完成。



在调用 RunWorkerAsync()之前,您应该调用 class1BindingSource.SuspendBinding()并且在您的 RunWorkerCompleted 中,您应该调用 class1BindingSource.ResumeBinding()



还要确保在您的 DoWork 中,您不会在绑定源上调用任何方法(像你这样使用 bs.ResetItem(0))。



还要删除这个lock语句。考虑使用一些私有对象_Gate = new Object(); ,它根本没有任何意义(在你的例子中),如果你真的需要它在课堂内避免外界的任何僵局,因为 bs.SyncRoot 是公开的。


We are binding a DataGridview using BindingSource. So in the main thread we have given like this.

            class1BindingSource = new BindingSource();
            class1BindingSource.DataSource = class1List;  
            this.dataGridView1.DataSource = class1BindingSource;

After that i have a placed a background worker in the form and is triggering in a button click.

i.e. in the button click

this.backgroundWorker1.RunWorkerAsync()

In the BackgroundWorker DoWork Event i am trying to update the BindingSource and there by trying to update the DataGridview.

So the BindingSource reset is done in a method in another class.

DoWork Event

Class2 cl2 = new Class2();
cl2.UpdateBindingSource(class1BindingSource);

UpdateBindingSource Method

public void UpdateBindingSource(BindingSource bs)
        {
            Class1 c1 = bs.Current as Class1;    
            for (int i = 0; i < 1000; i++)
            {
                lock (bs.SyncRoot)
                {
                    c1.MyProperty1 = i;
                    bs.ResetItem(0);
                }
            }
        }

Now i am getting an exception like BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refer back to BindingSource.

If i am doing this in my DoWork Event then i can reset the item in the control thread itself using BeginInvoke method.

But actually i am trying to simulate our application scenario. So i want to solve this in this format.

Can any one help me on this.

解决方案

The problem is that you can't update a BindingSource within another thread than the gui thread. This is due the fact, that the BindingSource will fire some events which will then be received by your data grid view which will then start to update itself, which will fail cause it won't be done on the gui thread.

So right before you call RunWorkerAsync() you should call class1BindingSource.SuspendBinding() and within your RunWorkerCompleted you should call class1BindingSource.ResumeBinding().

Also ensure that within your DoWork you won't call any methods on the binding source (like you did with bs.ResetItem(0)).

And also remove this lock statement. It simply doesn't make any sense (in your example) and if you really need it (in your real code) consider using some private object _Gate = new Object(); within your class to avoid any deadlocks from the outer world, cause bs.SyncRoot is publicly available.

这篇关于“BindingSource不能是其自己的数据源” - 尝试从另一个类中的方法重置绑定源时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆