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
Loading
Bechir BejaouiPosted Feb 26, 2009, 5:55 AM
Swaroop ChakrvartyPosted Feb 25, 2009, 8:28 AM
Bechir BejaouiPosted Feb 23, 2009, 5:38 AM
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
srungarakavi bhanuprakashPosted Feb 20, 2009, 5:51 AM
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........
Bechir BejaouiPosted Feb 19, 2009, 6:07 PM
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;