开发者

Unable to cast object of type 'system.data.datatable' to type

开发者 https://www.devze.com 2023-01-12 22:52 出处:网络
I have a datatable AVT of type开发者_C百科 AvailDataTable.The following code works to create a new datatable from AVT:

I have a datatable AVT of type开发者_C百科 AvailDataTable. The following code works to create a new datatable from AVT:

AvailDataTable AVT1 = (AvailDataTable) AVT.DefaultView.Table;

However, the code:

AvailDataTable AVT1 = (AvailDataTable) AVT.DefaultView.ToTable();

fails with the message "Unable to cast object of type 'system.data.datatable' to type 'AvailDataTable'.

Can someone explain why the first cast works but the second one doesn't? Thanks!


DataView.Table returns you the underlying source DataTable, in your case this source table is of type AvailDataTable.

http://msdn.microsoft.com/en-us/library/system.data.dataview.table.aspx

Whereas DefaultView.ToTable constructs a new instance of a DataTable based on the rows in the current view.

Subtle difference here but ToTable returns you a generic DataTable object not your specific DataTable type

http://msdn.microsoft.com/en-us/library/a8ycds2f.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消