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
BindingSourcewithin a thread other than the gui thread. This is due the fact, that theBindingSourcewill 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 callclass1BindingSource.SuspendBinding()and within yourRunWorkerCompletedyou should callclass1BindingSource.ResumeBinding().Also ensure that within your
DoWorkyou won’t call any methods on the binding source (like you did withbs.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, causebs.SyncRootis publicly available.