无法将类型为“IList<Class>”的对象强制转换为类型“System.Data.DataRowView” 。的解决方法
Posted on 2009-04-22 12:05 billpeng 阅读(868) 评论(0) 编辑 收藏 举报
今天把Repeater的数据源从DataTable换成了IList,结果Repeater_ItemDataBound事件中的代码无法通过,提示:无法将类型为“Bangso.Module.Feeds”的对象强制转换为类型“System.Data.DataRowView”。
DataRowView dv = ((DataRowView)e.Item.DataItem);
经查,解决方法如下 :
原来:
protected void Repeater5_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dv = ((DataRowView)e.Item.DataItem);
((Literal)e.Item.FindControl("Feeds")).Text = dv.Row["Types"].ToString() + "xxx";
}
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dv = ((DataRowView)e.Item.DataItem);
((Literal)e.Item.FindControl("Feeds")).Text = dv.Row["Types"].ToString() + "xxx";
}
}
改为:
protected void Repeater5_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Bangso.Module.Feeds dv = (Bangso.Module.Feeds)e.Item.DataItem;
((Literal)e.Item.FindControl("Feeds")).Text = dv.Types + "xxx";
}
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Bangso.Module.Feeds dv = (Bangso.Module.Feeds)e.Item.DataItem;
((Literal)e.Item.FindControl("Feeds")).Text = dv.Types + "xxx";
}
}