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
精彩评论