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 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.
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.
I had the same problem: - BindingSource that had elements with INotifyPropertyChanged - A separate Task that updated the elements.
The suggested solutions SuspendBinding etc didn't work. BindingSource should have done something like IsInvokeRequired.
Luckily Ivan Stoev came with the brilliant idea of subclassing the BindingSource and do something similar as IsInvokeRequired. Thank you Ivan!
Link: Update BindingSource from a different Task
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With