c# - How can I loop through a DataGridView created on the main form in a BackgroundWorker? -


I want to loop through a datagrid view that is mainly exporting data to a CSV file in a background vendor Designed for Backgroundworker has been made on a different form where the progress of exports will be displayed through the progress bar. Here is the export form code, which is called BackgroundWorker:

  Private Data Grid View Export Grid; Public Zero Export CSG (DataGrid View Main Grid) {this.exportGrid = mainGrid; // Set progress bar maximum progress. Bayer 1 Max = main grid Rows. Calculation; If (backgroundWorker1.IsBusy! = True) {// Start Asynchronous Operation Background Worker 1. RunWorkerAsync (); } // Show this look ShowDialog (); } Private zero background by 1_DoWork (Object Sender, DoWorkEventArgs e) {BackgroundWorker worker = Sender as BackgroundWorker; // Write data rows foreach (DataGridViewRow line in ExportGrid.Rows) {// Check if the background worker has been canceled (worker.CancellationPending == true) {e.Cancel = true; break; } Else {foreach (DataGridViewCell cell in row.Cells) {if (cell.Visible) {// Do CSV is writing here ...}} // Report current progress of updating UI worker. Report progress (r. Index + 1); }}} 1_ProgressChanged by Private Zero Background (Object Sender, Progress Changing EventArgs E) {// Update progress bar this.progressBar1.Value = e.ProgressPercentage; } Private Zero Background Worker 1_Workers (Object Sender, Runwalker, Comprehensive Advertising E.) {// Close the form when the background worker completes. Stop it (); }   

This code is causing the following errors:

  • The binding source can not be its own data source DataSource and DataMember properties set to those values Do not revert back to BindingSource.
  • Cross-thread operation is not valid: Control 'mainGrid' was created from the thread on which it was created. >

    I think these are because I am accessing the DataGrid view in a thread that it was not created. What is the best way to go about doing this? Is this also possible?

    Update:

    I'm looping through the datagrid view instead of the data source, that the user will be changing the column order, sorted And show / hide grid columns and they want them to be reflected in the export data. Is this a different way of handling it?

    Jeff, in my opinion, you are making at least two mistakes

      < Li> Exporting data from a UI control instead of doing it from the data source;
    • Trying to access the UI controls from the background thread;

      I will not try to use just one UI control (in your case grid) in a form which is not even in a form where the background thread is declared, the code So will be clear and unreadable ...

      Then UI controls are used to render data in the UI; Whenever you need to access data for anything other than rendering in the screen, you can better access the data source used to populate the UI.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -