troubleshooting Question

Threading Problem Updating Datatable and Refreshing DataGridView

Avatar of rogerard
rogerardFlag for United States of America asked on
Programming TheoryAlgorithmsVisual Basic.NET
13 Comments1 Solution5490 ViewsLast Modified:
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?


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros