8

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.

spajce
  • 7,044
  • 5
  • 29
  • 44
Mahesh KP
  • 6,248
  • 13
  • 50
  • 71

3 Answers3

18

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.

Welton v3.62
  • 2,210
  • 7
  • 29
  • 46
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • So where i update the binding source and there by rebind the gridview? – Mahesh KP Jan 11 '13 at 09:08
  • i understood that error is causing when trying to update the datasource from another thread ie not from the UI thread. Is there any way to invoke or call gridview.BeginInvoke from this method? i think may that will also solve the problem.. – Mahesh KP Jan 11 '13 at 09:32
  • @mahesh: Remove the `lock` and the `bs.ResetItem()` call; encapsulate the `c1.MyProperty1 = i' into a `Invoke()` call. But this can lead to other problems, cause you're steadily jumping between two threads. So it's better to suspend the binding source, do the stuff in background and resume the binding when it's finished. – Oliver Jan 11 '13 at 11:33
  • Sorry, i didn't understand, how do i encapsulate the c1.MyPropert1=i to an invoke call.Since the whole thing is in another class. Also if i remove ResetItem(), then how will it rebind the gridview? – Mahesh KP Jan 11 '13 at 11:41
3

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

Community
  • 1
  • 1
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
0

UpdateBindingSource() does not take much time, so no need to use backgroundworker. You can invoke UpdateBindingSource() in the main thread. Also, keep datagridview manipulation in the main thread.

Ahmad Farid
  • 14,398
  • 45
  • 96
  • 136
Kamil
  • 594
  • 1
  • 8
  • 28