1
Answer

How to get item value from gridview use linq binding?

Le  Ngoc Anh

Le Ngoc Anh

14y
7.2k
1
I use linq bind data to gridview, code behide that:

     public partial class WebForm1 : System.Web.UI.Page
{
DataContext sqlContext = new DataContext();
protected void Page_Load(object sender, EventArgs e)
{
var _Agent = from c in sqlContext.Agents
select c;
GridView1.DataSource = _Agent;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
RadioButtonList _rblGender = (RadioButtonList)e.Row.FindControl("
rblGender");
_rblGender.SelectedValue = dRowView[3].ToString();
}
}
}
}

I can't get value from gridview. It return error:

Unable to cast object of type '<>f__AnonymousType0`11[System.Int32,System.String,System.String, System.String,System.String,System.String,System.String,System.Nullable`1[System.DateTime], System.String,System.String,System.String]' to type 'System.Data.DataRowView'.


 
Line 28:         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
Line 29: {
Line 30: DataRowView dRowView = (DataRowView)e.Row.DataItem;
Line 31: if (e.Row.RowType == DataControlRowType.DataRow)
Line 32: {

Please help me!

Thanks

Answers (1)