Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6386165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:59:24+00:00 2026-05-25T02:59:24+00:00

I want to loop through a DataGridView that is created on the main form

  • 0

I want to loop through a DataGridView that is created on the main form in a BackgroundWorker to export the data to a CSV file. The BackgroundWorker is created on a separate form where the progress of the export will be displayed via a progress bar. Here is the code on the export form that calls the BackgroundWorker:

private DataGridView exportGrid;

public void ExportCSV(DataGridView mainGrid)
{
    this.exportGrid = mainGrid;

    //Set progress bar maximum
    progressBar1.Maximum = mainGrid.Rows.Count;

    if (backgroundWorker1.IsBusy != true)
    {
        //Start the asynchronous operation
        backgroundWorker1.RunWorkerAsync();
    }

    //Show the form
    this.ShowDialog();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    //Write data rows
    foreach (DataGridViewRow row in exportGrid.Rows)
    {
        //Check if the background worker has been cancelled
        if (worker.CancellationPending == true)
        {
            e.Cancel = true;
            break;
        }
        else
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if (cell.Visible)
                {
                    //Do CSV writing here...
                }
            }

            //Report current progress to update UI
            worker.ReportProgress(row.Index + 1);
        }
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //Update progress bar
    this.progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //Close the form once the background worker is complete
    this.Close();
}

This code has been causing the following errors:

  • BindingSource cannot be its own data source. Do not set the
    DataSource and DataMember properties to values that refer back to
    BindingSource.
  • Cross-thread operation not valid: Control ‘mainGrid’ accessed from a
    thread other than the thread it was created on.

I assume that these are because I am accessing the DataGridView in a thread that did not create it. What is the best way to go about doing this? Is it even possible?

Update:

The reason I am looping through the DataGridView instead of the datasource is that the users will be changing the column order, sort order and showing/hiding columns of the grid and they want these changes reflected in the exported data. Is there a different way to handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:59:25+00:00Added an answer on May 25, 2026 at 2:59 am

    Jeff in my opinion you are doing at minimum two mistakes in here:

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

    I just would not try to access a UI control (Grid in your case) in a form which is not even the form where the background thread is declared, code will be so unclear and unreadable…

    then consider that UI controls are used to render data in the UI; whenever you need to access the data for anything else than rendering in the screen you’d better access directly the datasource used to populate the UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of elements that i want to loop through as groups.
I want to loop through the lines on a file, and match as regular
I want to loop through a xml file and display the values in text
I want to loop through my datatable column called SDESCR and created a string
I have a directory of pictures that I want to loop through and do
I want to loop through a vector and erase certain elements that correspond to
I want to loop through options in the select box that has attribute set
I have a List, and I want to loop through that List and remove
I'm writing a batch file for windows command prompt, I want to loop through
<fx:XML id=dataGridDataFieldsXML source=assets/dataGridDataFieldsXML.xml/> <s:XMLListCollection id=showGridFields source={dataGridDataFieldsXML.viewer}/> I want to loop through my XML file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.