Hi VM,
"VM" <vo******@yahoo.com> wrote in message
news:uL**************@tk2msftngp13.phx.gbl...
In the thread, I create a tableX and fill it with data. When I set the
grid's source to this tableX, I get a compiler error "Controls created on
one thread cannot be parented to a control on a different thread". I think
this occurs because the grid wasn't created during the thread. How would I
be able to set its datasource from within the thread?
Thanks again.
Your diagnosis is correct. Basically, you should only call methods or
get and set properties of forms or controls from the thread that created the
form or control. Usually you don't get such a nice explicit error, just
flakey or unstable behavior. The work-around is:
1. Create a method that will set the property on the control.
2. Create a delegate object that references the method.
3. Call Control.Invoke on the form or control, passing the delegate
object along with any necessary parameters.
An example (assumes code is within a form):
...
private delegate void SetGridDataSourceHandler(object dataSource);
private void SetGridDataSource(object dataSource)
{
if (this.InvokeRequired)
{
SetGridDataSourceHandler handler =
new SetGridDataSourceHandler(this.SetGridDataSource);
this.Invoke(handler, new object[]{dataSource});
}
else
{
this.dataGrid1.DataSource = dataSource;
}
}
...
DISCLAIMER: The above code is off-the-top-of-my-head and may contain
syntax or other obvious errors.
Regards,
Daniel