Skip to content

Using Backgroundworker thread to populate Data to a DataGridView

srungarakavi bhanuprakash

Asked Feb 18, 2009, 10:58 PM EST5 answers
Share
LinkedInX
Hi All,

I have problem while using a backgroundworker Thread for loading a xml file to a datagridview.
My form contains a datagridview.
I am trying to populate a xml file to a datagridview.
xml file contains huge data. So when I am trying to load the file, its taking lot of time to load on to a datagridview.
So I tried using backgroundworker thread to load the file into a datagridview.

here is the code..

[code]
private void DataViewer_Load(object sender, EventArgs e)
        {
               backgroundWorkerThread.RunWorkerAsync();                     
        }

private void backgroundWorkerThread_DoWork(object sender, DoWorkEventArgs e)
        {
               LoadingDataGrid();
        }

private void LoadingDataGrid()
         {
                   dataset ds = new Dataset();              //----------                        line 1
                   ds.ReadXml("temp.xml");                 //----------                        line 2
                   datatable dt = new datatable();         //----------                        line 3
                   dt = ds.table[0];                               //----------                       line 4
                   datagridview.datasource = dt;          //----------                       line 5
         }
[/code]

I got a exception at  line 5 as ..

error :

[B]System.InvalidOperationException: Cross-thread operation not valid: Control 'dataGridView' accessed from a thread other than the thread it was created on.
[/B]   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
   at System.Windows.Forms.Control.BeginUpdateInternal()
   at System.Windows.Forms.DataGridView.RefreshColumns()
   at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.set_BindingContextInternal(BindingContext value)
   at System.Windows.Forms.ContainerControl.set_BindingContext(BindingContext value)
   at System.Windows.Forms.ContainerControl.get_BindingContext()
   at System.Windows.Forms.Control.get_BindingContextInternal()
   at System.Windows.Forms.Control.get_BindingContext()
   at System.Windows.Forms.Control.get_BindingContextInternal()
   at System.Windows.Forms.ContainerControl.get_BindingContext()
   at System.Windows.Forms.Control.get_BindingContextInternal()
   at System.Windows.Forms.Control.get_BindingContext()
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.SetDataConnection(Object dataSource, String dataMember)
   at System.Windows.Forms.DataGridView.set_DataSource(Object value)


Is this the correct process to use backgroundworker thread to load data to a datagridview or , some other process is there ??
If so please provide me with a sample program.

Could someone help me out with this issue.

In Advance thanks

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    Try to change this code

    private void backgroundWorkerThread_DoWork(object sender, DoWorkEventArgs e)
            {
                   LoadingDataGrid();
            }

    private void LoadingDataGrid()
             {
                       dataset ds = new Dataset();              //----------                        line 1
                       ds.ReadXml("temp.xml");                 //----------                        line 2
                       datatable dt = new datatable();         //----------                        line 3
                       dt = ds.table[0];                               //----------                       line 4
                       datagridview.datasource = dt;          //----------                       line 5
             }

    by this one


    DataTable mySource;

    private void backgroundWorkerThread_DoWork(object sender, DoWorkEventArgs e)
            {
                 
    mySource = LoadingDataGrid();
                 
            }

    private DataTable LoadingDataGrid()
             {
                       dataset ds = new Dataset();              //----------                        line 1
                       ds.ReadXml("temp.xml");                 //----------                        line 2
                       datatable dt = new datatable();         //----------                        line 3
                       dt = ds.table[0];                               //----------                       line 4
                        return dt;          //----------                       line 5
             }


    In the load event handler you set

    datagidview.DataSource = mySource;
    0
  • 0
    Hey thanks for your help,

    Now its working fine, but while populate a datatable to a datagridview, its taking lot of time.

    datatable mysource;
    datagridview = mysource;  (at this point).

    Is there a possible way to load few table at time, like when we open a MS WORD Document and load a file into it. Initially it will load few data into a word doc and in background it loads the hole file. Even Images also open in background.

    Could someone help me with this issue. If possible provide me a sample code or reference.

    Thanks in Advance........
    0
  • Bechir Bejaoui
    0
    You can Implement an Asynchrone logic, I mean let the data been loaded meanwhile the user does other tasks until the main task is completed. The .Net provides an API called APM "Asynchrone Programing Mode" I give you and example, if you you use the command object to execute a request the  command object also proposes asynchrone mode, if you look at the intellisense you will see something like Begin<method name> which returns an IAsyncResult object and End<method name> which returns an integer
    http://msdn.microsoft.com/fr-fr/library/1a674khd(VS.80).aspx

    What I suggest too is that you implement a progress bar to indicate to the user the task progress
    0
  • 0
    Add Control.CheckForIllegalCrossThreadCalls = false; in the Costructor
    0
  • Bechir Bejaoui
    0
    Well I'm not totally agree, providing a thread safe code is the best solution
    0