We help IT Professionals succeed at work.
Get Started

Threading Problem Updating Datatable and Refreshing DataGridView

Last Modified: 2013-11-11
I have a DataGridView (dgv) whose DataSource is a BindSource Control whose DataSource is a Dataset and DataMember is a DataTable (dtMain).
Private Delegate sub AddEntryToTableCallback(Byval Entry as SearchResultEntry)

Private Sub bwWorker_DoWork(ByVal sender as Object, ByVal e as DoWorkEventArgs) Handles bwWorker.DoWork

  Dim AddEntry As New AddEntryToTableCallback(AddressOf AddEntryToTable)

  '.... Some worker code here...'

  While True

    For Each entry as SearchResultEntry in Response.Entries

  End While

End Sub

Private Sub AddEntryToTable(ByVal Entry as SearchResultEntry)
  Dim tmpDataRow as DataRow = dtMain.NewRow

  '..Assign values to datarow....'


  if Me.InvokeRequired Then
    Me.BeginInvoke(New MethodInvoker(AddressOf RefreshDisplay))
  End If
End Sub

Private Sub RefreshDisplay()

    If me.InvokeRequired Then
      me.Invoke(New MethodInvoker(AddressOf dgv.Refresh))
    End If

End Sub

Open in new window

If I manipulate dgv too much (ie resize, scroll through the added results) while it's still adding, I'll get several of these errors:
DataGridView Default Error Dialog

The following exception occured in the DataGridView:

System.InvalidOperationException:  BindingSource cannot be its own data source.  Do not set the DataSource and Datamember Properties to values that refer back to the BindingSource.
  at System.Windows.Forms.BindingSource.get_Count()
  at System.Windows.Forms.CurrencyManager.get_Item(Int32_index)
  at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)

To replace this default dialog please handle the DataError Event.

Open in new window

It isn't exactly clear where this is getting thrown and I've tried adding a catch for the DataError Event, but I can't Isolate it.  The error doesn't seem to hinder the results from continuing to be added.  Nor does the error occur at any time after all rows have been added.  

Does anyone have any thoughts how I can catch and handle/ignore the error?

Watch Question
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE