Reputation: 2534
I have 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?
Upvotes: 8
Views: 53705
Reputation: 1
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;
Upvotes: 0
Reputation: 11
You can cast like:
datarow= DirectCast (System.Data.DataRowView, System.Data.DataRow).row
Upvotes: 1
Reputation: 292725
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;
...
}
Upvotes: 21
Reputation: 33484
Instead of foreach(DataRow dr in myDataView)
, try
foreach(DataRowView drv in myDataView)
.
Upvotes: 0
Reputation: 16032
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
.
Upvotes: 0