Cast from Object to DataSEt

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to put the Data Source of a DataGrid into a DataSet variable. I have
tried the follwoing code but I get and error:

DataSet ds = (DataSet)Convert.ChangeType(this.dgChgMgmt.DataSource,
typeof(DataSet));

The error is:
A first chance exception of type 'System.InvalidCastException' occurred in
mscorlib.dll

How can I fix this? I am using Framework 1.1.
 
You should be able to just use

DataSet ds = (DataSet) this.dgChgMgmt.DataSource;

unless you are doing some really off things ... are you sure that your
DataSource is a dataset and not say a datatable?

What does ...

Console.WriteLine( this.dgChgMgmt.DataSource.GetType().ToString()) print
out?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 
You are correct on both counts. I am binding to a table and I was trying to
make the code more difficult than it had to be.

Thanks for you helpful response.
--
Robert Hill



Greg Young said:
You should be able to just use

DataSet ds = (DataSet) this.dgChgMgmt.DataSource;

unless you are doing some really off things ... are you sure that your
DataSource is a dataset and not say a datatable?

What does ...

Console.WriteLine( this.dgChgMgmt.DataSource.GetType().ToString()) print
out?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 

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

Back
Top