Using Backgroundworker thread to populate Data to a DataGridView

Feb 19 2009 3:58 AM
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

Answers (5)