我使用LINQ to SQL中填写一个GridView:无法投匿名对象System.Data.DataRowView
var results = from r in db.MyForm1_hosps
where r.recordId == recordId
orderby r.hospId
select new { r.hospId, r.which, r.description };
if (results.Count() > 0)
{
Form_1_hosp.DataSource = results;
Form_1_hosp.DataBind();
}
OnRowDataBound在以后,我调用下面的代码填写在RadioButtonList的
的价值if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rbl = e.Row.FindControl("which") as RadioButtonList;
if (rbl != null)
{
DataRowView rowView = (DataRowView)(e.Row.DataItem);
LoadRadioButtonList(rowView["which"], rbl);
}
}
我得到以下错误:
Unable to cast object of type '<>f__AnonymousType1`3[System.Int32,System.Int16,System.String]' to type 'System.Data.DataRowView'.
我明白,一个匿名的对象不能投到datarowview,但我可以施加什么,以获得“哪个”的值
这看起来不错。我不会在星期一之前尝试这种方式,并且如果它有效(它应该)会标记为已回答。 – peroija 2012-02-17 23:45:43
都有效。因为我有5个gridviews,我不想创建5个类,所以我选择了第二个。谢谢! – peroija 2012-02-20 13:25:46