Updating DataGrid From a BackGroundWorker


I hava a Background Worker and a DataGrid in my c# Application. In do work of my Backgroundworker which will call an Api in my dlls which will enter some Data in a SQLite Database. After the Completion of my Api call I report a progress and In progress event of my Backgroundworker I get the contents from Db and assign it as a DataSource to my grid. I call same API in same backgroundworker. In the middle of processing my application crashes. But If I dont assign the dataSource in ProgressChanged my application doesnt crashes.


I am assuming you must be accessing UI object using Invoke method.

If not try using following approach (Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.):

//In Form.Designer.cs Label myLabel = new Label(); //In code behind under Background worker method LabelVlaueSetter SetLabelTextDel = SetLabelText; if (myLabel .InvokeRequired) { myLabel.Invoke(SetLabelTextDel, "Some Value"); } private delegate void LabelVlaueSetter(string value); //Set method invoked by background thread private void SetLabelText(string value) { myLabel.Text = value; }


As Johnathan Allen mentions, it should not matter. Unless something else is going on. I have had two cases where I could not interact with certain controls in the events generated by the BackgroundWorker. The only thing that worked was using the Invoke method.

Try assigning the DataSource on the same thread that created the DataGridView control. You do this through the control's Invoke method. Use the following code. (I have not tested, but this is the standard pattern.)

If this does not work then try Jonathan Allen's suggestion.

Actually, do whichever suggestion is easiest to try first.

<hr /> private delegate void SetDataSourceDelegate(object value); private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { DataTable oData = null; //'assign data source if (dataGridView1.InvokeRequired) { dataGridView1.Invoke(new SetDataSourceDelegate(SetDataSource), new Object[] {oData}); }else{ SetDataSource(oData); } } private void SetDataSource(object value) { dataGridView1.DataSource = value; }


It shouldn't matter, but why are you using ProgressChanged instead of RunWorkerCompleted?

Also, try doing everything on the GUI thread without the BackgroundWorker. That will let you know if the problem is in your code or how your code interacts with the GUI.


  • How to prevent cross domain issues by proxying in IIS?
  • Performance difference between accessing local and class member variables
  • Access PCF DEV from external machine on same network as host
  • cd command not working with execvp
  • ConfigurationBuilder not working in azure function
  • Python PIL to extract number from image
  • Problems with toDataURL HTML5 other ways to get canvas data?
  • Using MouseListener to select a range of cells in a grid
  • Find JSON nested nodes using multiple string values
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Optimizing the print function in Matlab
  • URLConnection doesn't work since API 10 and higher?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Security issues with PHP's Readfile method
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Stop Bash Script if Hive Fails
  • OOP Javascript - Is “get property” method necessary?
  • debug library loaded with ctypes using gdb
  • JQuery Internet Explorer and ajaxstop
  • Use of this Javascript
  • Cancel a live stream “fast motion” catch-up in Flash
  • Spring Data JPA custom method causing PropertyReferenceException
  • Change JButton Shape while respecting Look And Feel
  • C# - Serializing and deserializing static member
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Sending data from AppleScript to FileMaker records
  • Trying to switch camera back to front but getting exception
  • jQuery tmpl and DataLink beta
  • AT Commands to Send SMS not working in Windows 8.1
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • How to include full .NET prerequisite for Wix Burn installer
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Programmatically clearing map cache
  • Unable to use reactive element in my shiny app
  • How to load view controller without button in storyboard?