Problem Hiding Columns when Populating a Datagrid with a DataView of A DataTable From Dataset return

B

Bob Davies

Hi

I have a webservice that retrieves data from a database, this is then
returned to the calling client application built in windows forms within a
dataset, however upon attempting to create tablestyles to format any of the
columns within the datagrid, the exception "Can-not parent objects created
on one thread to objects created on another" or words to that effect.

I'm not too sure if what I said make sense, but i will add details to
clarify.

I have an MDI Windows Forms interface, which contains a Method called
CommunicateWithDatabase(args) this then sends the datarequest to the
webservice (tryed both synchronous, asynchronous methods, including creating
the calling method on a different thread and not on a different thread to
the UI thread). Any sub-forms mdiparent'ed to the main form request data via
MainForm.CommunicateWithDatabase(args).

The data returned from the web-service is held within a DataSet, and all the
data is present in memory using any retrieval/access method.
DataSet.Tables[0] is passed into the subform as a DataTable which is then,
within a control containing a standard DataGrid and DataView is set as the
source of the DataView (Datagrid.Datasource = Dataview; DataView.DataSource
= DataSet.Tables[0])

Once the datasource of the dataview is set, and thus the datagrid. I proceed
to hide columns within the datagrid using the addition of tablestyles with
columnwidth set to 0 or otherwise for non-hidden. This is where the problem
occurs.

Essentially the problem is currently seen to be that the new tablestyles
can't be created on the datagrid as the underlying dataset was created on
the web-service thread and the new tablestyles has been created on the
windows forms UI thread. I have also tried to pass a dataset into the
webservice and retrieve the same one, but i assumed this wouold not work and
I was seemingly correct about this. As well as having tried a .copy of the
dataset into a locally created new dataset.

It is essential that i use this method of accessing the database via a
web-service for security reasons. However the rest of the interface is still
changeable.

Does anyone know of any way to allow tablestyles to be created on a datagrid
referencing a dataset/datatable created on a different thread? or a
different workaround for this problem. Please feel free to email me
regarding this issue or any associated topic that may be of use, this is now
becoming a huge problem for this particular development cycle :(

Thanks Very much for any help.
Bob
(e-mail address removed)
 
S

Sijin Joseph

The message suggests that the problem is coming because you are setting
the table style of the datagrid in a thread different from the one on
which the the datagrid was created.

To check if this is the case, check the datagrid.InvokeRequired property
to see if you need to use Control.Invoke() to set the tablestyles.

Also check out this KB article which deals with the same issue
http://support.microsoft.com/kb/318607/EN-US/

Hope that helps.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Bob said:
Hi

I have a webservice that retrieves data from a database, this is then
returned to the calling client application built in windows forms within a
dataset, however upon attempting to create tablestyles to format any of the
columns within the datagrid, the exception "Can-not parent objects created
on one thread to objects created on another" or words to that effect.

I'm not too sure if what I said make sense, but i will add details to
clarify.

I have an MDI Windows Forms interface, which contains a Method called
CommunicateWithDatabase(args) this then sends the datarequest to the
webservice (tryed both synchronous, asynchronous methods, including creating
the calling method on a different thread and not on a different thread to
the UI thread). Any sub-forms mdiparent'ed to the main form request data via
MainForm.CommunicateWithDatabase(args).

The data returned from the web-service is held within a DataSet, and all the
data is present in memory using any retrieval/access method.
DataSet.Tables[0] is passed into the subform as a DataTable which is then,
within a control containing a standard DataGrid and DataView is set as the
source of the DataView (Datagrid.Datasource = Dataview; DataView.DataSource
= DataSet.Tables[0])

Once the datasource of the dataview is set, and thus the datagrid. I proceed
to hide columns within the datagrid using the addition of tablestyles with
columnwidth set to 0 or otherwise for non-hidden. This is where the problem
occurs.

Essentially the problem is currently seen to be that the new tablestyles
can't be created on the datagrid as the underlying dataset was created on
the web-service thread and the new tablestyles has been created on the
windows forms UI thread. I have also tried to pass a dataset into the
webservice and retrieve the same one, but i assumed this wouold not work and
I was seemingly correct about this. As well as having tried a .copy of the
dataset into a locally created new dataset.

It is essential that i use this method of accessing the database via a
web-service for security reasons. However the rest of the interface is still
changeable.

Does anyone know of any way to allow tablestyles to be created on a datagrid
referencing a dataset/datatable created on a different thread? or a
different workaround for this problem. Please feel free to email me
regarding this issue or any associated topic that may be of use, this is now
becoming a huge problem for this particular development cycle :(

Thanks Very much for any help.
Bob
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top