Cast DataView as a DataTable

  • Thread starter Thread starter Brian Bischof
  • Start date Start date
B

Brian Bischof

I'm using a third-party tool that takes a DataTable as a parameter. I really
need to pass it a DataView instead. When I try to explicitly cast the
DataView as a DataTable I get an error that they aren't compatible data
types. Does anyone have a suggestion on how to do this?

My last resort is to create a new DataTable object and copy the rows from
the DataView into the DataTable. But this is an ugly hack and I really hope
there is a better solution.

Thanks.
 
Can't do it. You can access the table that is part of the DataView using the
DataView.Table property, but otherwise they're two way different beasties so
you can't cast them.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP- FrontPage
 
Thanks for the info. I wrote a method to create a new table based off the
original table structure and then add the rows into it. Lots of loops!

Brian
 
Brian,
You do realize that you can use DataTable.Clone to "create a new table based
off the original table structure"?

Then you only need a single loop to add the respective rows to the new
DataTable.

Unless of course you are modifying the structure itself of the original
DataTable.

BTW: DataTable.Clone will copy the structure of an existing DataTable, while
DataTable.Copy will copy the structure & data of an existing DataTable.

Hope this helps
Jay
 
Back
Top