“BindingSource 不能是它自己的数据源";- 尝试从另一个类中的方法重置绑定源时出错 [英] "BindingSource cannot be its own data source" - error when trying to reset the binding source from a method in another class
问题描述
我们正在使用 BindingSource
绑定一个 DataGridview
.所以在主线程中我们是这样给出的.
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.
即在按钮中点击
this.backgroundWorker1.RunWorkerAsync()
在 BackgroundWorker
DoWork Event
中,我尝试更新 BindingSource
并尝试更新 DataGridview
>.
In the BackgroundWorker
DoWork Event
i am trying to update the BindingSource
and there by trying to update the DataGridview
.
所以 BindingSource
重置是在另一个类的方法中完成的.
So the BindingSource
reset is done in a method in another class.
DoWork 事件
Class2 cl2 = new Class2();
cl2.UpdateBindingSource(class1BindingSource);
UpdateBindingSource 方法
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);
}
}
}
现在我收到一个异常,例如 BindingSource
不能是它自己的数据源.不要将 DataSource
和 DataMember
属性设置为引用回 BindingSource
的值.
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
.
如果我在我的 DoWork Event
中执行此操作,那么我可以使用 BeginInvoke 方法
重置控制线程本身中的项目.
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.
推荐答案
问题是您无法在 gui 线程之外的线程中更新 BindingSource
.这是因为 BindingSource
将触发一些事件,然后您的数据网格视图将接收这些事件,然后这些事件将开始自我更新,这将失败,因为它不会在gui线程.
The problem is that you can't update a BindingSource
within a thread other 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.
所以在你调用 RunWorkerAsync()
之前你应该调用 class1BindingSource.SuspendBinding()
并且在你的 RunWorkerCompleted
中你应该调用 class1BindingSource.ResumeBinding()
.
So right before you call RunWorkerAsync()
you should call class1BindingSource.SuspendBinding()
and within your RunWorkerCompleted
you should call class1BindingSource.ResumeBinding()
.
还要确保在您的 DoWork
中您不会调用绑定源上的任何方法(就像您对 bs.ResetItem(0)
所做的那样).
Also ensure that within your DoWork
you won't call any methods on the binding source (like you did with bs.ResetItem(0)
).
并删除此锁定语句.它根本没有任何意义(在您的示例中),如果您真的需要它(在您的真实代码中),请考虑在您的类中使用一些 private object _Gate = new Object();
以避免任何来自外部世界的死锁,导致 bs.SyncRoot
是公开可用的.
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屋!