public static class EntityDataSourceHelper where TEntity : class
{
public static TEntity GetDataItemEntity(object dataItem)
{
var entity = dataItem as TEntity;
if (entity != null)
{
return entity;
}
var td = dataItem as ICustomTypeDescriptor;
if (td != null)
{
return (TEntity)td.GetPropertyOwner(null);
}
return null;
}
}
Calling this is simple....
protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
PersonEntity person= EntityDataSourceHelper< PersonEntity >. GetDataItemEntity(e.Row.DataItem);
}
}
This saves you from a couple of issues :
1. Referencing null column
2. Extra code to cater for null checks on rows
3. Extra code for casting etc
No comments:
Post a Comment