Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'
I h开发者_如何学Pythonave following exception in the foreach loop
Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'
How to solve the issue?
A DataRowView
is not a DataRow
, and isn't convertible to a DataRow
. However, you can access the corresponding DataRow
using the Row
property :
foreach(DataRowView drv in dataView)
{
DataRow row = drv.Row;
...
}
You can cast like:
datarow= DirectCast (System.Data.DataRowView, System.Data.DataRow).row
It looks like you used DataRow
as the foreach loop variable type while actually you are iterating enumerable with DataRowView
type. Check if DataRowView
has functionality that you need, if so you can just replace one with another. Or you can modify your code to return enumerable with DataRow
instead of one with DataRowView
.
Instead of foreach(DataRow dr in myDataView)
, try
foreach(DataRowView drv in myDataView)
.
I believe that you try to do something like this
DataRowView row = (DataRowView)e.Item.DataItem;
This Is How It Should Be Done
System.Data.Common.DbDataRecord objData = (System.Data.Common.DbDataRecord)e.Item.DataItem;
精彩评论