ASP.net GridView Shortcut
Monday, 5 June 2006
When dealing with Strongly Typed datasets on the ASP.net GridView 2.0, I found myself writing one too many lines of code like this:
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<%# DataBinder.Eval(
((System.Data.DataRowView) Container.DataItem).Row,
"CategoryRow.CategoryName") %>
</ItemTemplate>
<asp:TemplateField>
Solution?
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<%# EvalEx(Container.DataItem, "CategoryRow.CategoryName") %>
</ItemTemplate>
<asp:TemplateField>
protected object EvalEx(object DataItem, string Expression)
{
return DataBinder.Eval(((DataRowView) DataItem).Row, Expression);
}
No. 1 — June 5th, 2006 at 9:55 pm
Actually there’s a new “Eval” syntax in ASP.NET 2.0 to do this for you:
Cheers,
Marcie
No. 2 — June 5th, 2006 at 9:56 pm
Sorry, the code didn’t come through:
Eval(”CategoryRow.CategoryName”) %>
No. 3 — June 5th, 2006 at 10:00 pm
Actual Marcie, the Eval method in 2.0 didn’t quite work
Eval(”CategoryRow.CategoryName”)
ASP.net complains that there is no CategoryRow property.