InvalidCastException: (DataSet) DataGrid.DataSource

G

Guest

I need some help please.

Say you have a DataGrid and a DataSet, and you create a dataview to handle
sorting and filtering and then you assign the dataview to
DataGrid.DataSource. Once you did that you will get InvalidCastException if
you do this:

DataSet dataSet = (DataSet) dataGrid.DataSource;

But I need to do that because I need to get the DataGrid's dataentries in a
dataset and use XmlDataDocument to load the dataset and then write it to an
xml file.

Maybe my first design decision to assign dataview to the datasource should
be reviewed. I don't know. Any comment will really help.

Thank you.
 
R

recoil

Whenever you are getting an InvalidCast Exception it is always best to
see WHAT the 2 objects being casted to are. Use a debugger and find out
what is being casted to what. I
From the looks of it you are attempting to cast a DataView to a DataSet
which is NOT going to happen. If the Tables in the DataView have
changed then you need to get the new data from the DataView's Table. If
your data is complex then you may or may not have problems going from a
table where all data is represented by a flat table back to where the
data needs to be put in relational tables.

Hope there is enough in my rambling to help you find the path right.
 
G

Guest

I know why I'm getting the exception. Because I assign a DataView to
DataGrid.DataSource at the beginning when I attempt to filter and sort Xml
data and display it on dataGrid. But when I try to save the dataGrid back to
the Xml file I need to do the following to load it to XmlDataDocument.

XmlDataDocument doc = new XmlDataDocument( (DataSet) dataGrid.DataSource);

My question is how to resolve this problem.
 
G

Guest

I found the answer anyway. I need to use DataView.DataTable.DataSet
Thank you for your reply.
 

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