Click to See Complete Forum and Search --> : Using Backgroundworker thread to populate Data to a DataGridView


bhanuprakash_kavi
February 17th, 2009, 01:09 PM
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..

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
}


I got a exception at line 5 as ..

error :

System.InvalidOperationException: Cross-thread operation not valid: Control 'dataGridView' accessed from a thread other than the thread it was created on.
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 for helping me..

Mutant_Fruit
February 17th, 2009, 03:16 PM
That's because you're using the BackgroundWorker in exactly the way the documents say it cannot be used ;)

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Read there for more info. Basically you should be raising the WorkerCompleted event when the dataset is ready and from there you assign it to be the datasource for the datagrid.