473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

setting grid source within thread? Compiler error...

VM
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.
Nov 16 '05 #1
4 1111
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
Nov 16 '05 #2
VM
Thanks for the help. It worked great.

"Daniel Pratt" <ko******************@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #3
VM
One last question,
If I want to use this functionality in another class in the same
application, what namespaces would I need to add? In another class in this
same application (but abother project), it doesn't recognize this.Invoke.

Thanks agaim.

"Daniel Pratt" <ko******************@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #4
Hi VM,

"VM" <vo******@yahoo.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
One last question,
If I want to use this functionality in another class in the same
application, what namespaces would I need to add? In another class in this
same application (but abother project), it doesn't recognize this.Invoke.

Thanks agaim.


Forgive me if I misunderstand your question, but Invoke is a public
method of the System.Windows.Forms.Control class (System.Windows.Forms.Form
inherits System.Windows.Control). As such, you could call it from a
reference to a control or form.

Regards,
Daniel
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: John | last post by:
Hello. If I want to set up my first database and start using it in Dreamweaver what do I need to do? The book I'm working on has a CD with the database on. It is telling me to put it in the...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
2
by: VM | last post by:
I'm trying to dynamically display the rows in a datagrid while filling the datatable source. I'm doing it this way because we had already finished the app but this section sometimes took so long...
16
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.