protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
public void BindGridView()
{
List<Model> list = new List<Model>()
{
new Model(){id=1,startDate=DateTime.Now},
new Model(){id=2,startDate=DateTime.Now},
new Model(){id=3,startDate=DateTime.Now},
new Model(){id=4,startDate=DateTime.Now},
};
GridView1.DataSource = list;
GridView1.DataBind();
}
public class Model
{
public int id { get; set; }
public DateTime startDate { get; set; }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
Label startDatelbl = (Label)row.FindControl("startDateLabel");
var dd = ((Model)row.DataItem).startDate.ToString();
startDatelbl.Text = dd;
}
}
Resulting Page:
Hope this can help you.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
26 Points
155 Posts
Unable to cast object of type 'Model' to type 'System.Data.DataRowView'
Apr 15, 2020 02:03 PM|Ruffone|LINK
How do I grab the value from this code when my binding source is a List<model>
var dd = ((DataRowView)e.Row.DataItem)["startDate"].ToString();
Error
Unable to cast object of type 'Model' to type 'System.Data.DataRowView'
All-Star
194434 Points
28074 Posts
Moderator
Re: Unable to cast object of type 'Model' to type 'System.Data.DataRowView'
Apr 15, 2020 04:04 PM|Mikesdotnetting|LINK
var dd = ((Model)e.Row.DataItem).startDate;
Member
26 Points
155 Posts
Re: Unable to cast object of type 'Model' to type 'System.Data.DataRowView'
Apr 15, 2020 05:05 PM|Ruffone|LINK
Hay Mikesdtnetting, thanks for the response.
When I do that I get this error
'Unable to cast object of type 'System.Data.DataRowView' to type 'Model'.'
Contributor
2840 Points
840 Posts
Re: Unable to cast object of type 'Model' to type 'System.Data.DataRowView'
Apr 16, 2020 01:42 AM|Sean Fang|LINK
Hi Ruffone,
The reason of the problem is that you wrongly used the code "["startDate"]" to fetch the "startDate" field which should be a property of "Model".
The compiler can not identify what you want to treat the "e.Row.DataItem" as so it always produce an error.
I guess you are using a GridView Control to do the data-binding stuff. You could refer to below code to find more details.
.aspx: Auto Bind "id" but not for "startDate"
Code behind:
protected void Page_Load(object sender, EventArgs e) { BindGridView(); } public void BindGridView() { List<Model> list = new List<Model>() { new Model(){id=1,startDate=DateTime.Now}, new Model(){id=2,startDate=DateTime.Now}, new Model(){id=3,startDate=DateTime.Now}, new Model(){id=4,startDate=DateTime.Now}, }; GridView1.DataSource = list; GridView1.DataBind(); } public class Model { public int id { get; set; } public DateTime startDate { get; set; } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType == DataControlRowType.DataRow) { Label startDatelbl = (Label)row.FindControl("startDateLabel"); var dd = ((Model)row.DataItem).startDate.ToString(); startDatelbl.Text = dd; } }
Resulting Page:
Hope this can help you.
Best regards,
Sean