Skip to content

Instantly share code, notes, and snippets.

@orjan
Created October 7, 2009 21:19
Show Gist options
  • Save orjan/204456 to your computer and use it in GitHub Desktop.
Save orjan/204456 to your computer and use it in GitHub Desktop.
<%= Html.Grid(EntityEnumerator.GetEntities(Model)).WithModel(new AllPropertiesAsColumns<Entity>(EntityEnumerator.GetEntities(Model)))%>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using MvcContrib.UI.Grid;
using SharpArch.Core.DomainModel;
namespace SharpDojjo.Web.Helpers
{
public class EntityEnumerator
{
public static IEnumerable<Entity> GetEntities(object model)
{
foreach (var obj in (IEnumerable) model)
{
yield return (Entity) obj;
}
}
public static Type GetType(object o)
{
return null;
}
}
public class AllPropertiesAsColumns<T> : IGridModel<T> where T : class
{
private readonly IList<GridColumn<T>> columns;
private readonly IGridSections<T> sections = new GridSections<T>();
private Type getType(IEnumerable<Entity> enumerable)
{
foreach (var entity in enumerable)
{
return entity.GetType();
}
throw new Exception("N/A");
}
public AllPropertiesAsColumns(IEnumerable<Entity> enumerable)
{
Renderer = new HtmlTableGridRenderer<T>();
Attributes = new Dictionary<string, object>();
columns = new List<GridColumn<T>>();
Type type = getType(enumerable);
//PropertyInfo[] properties = typeof (T).GetProperties();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo info in properties)
{
if (info.CanRead)
{
var column = new GridColumn<T>
(ReflectedPropertyValue(info.Name), info.Name, typeof (T));
if (info.Name == "Id")
{
columns.Insert(0, column);
}
else
{
columns.Add(column);
}
}
}
}
#region IGridModel<T> Members
public IGridRenderer<T> Renderer { get; set; }
public ICollection<GridColumn<T>> Columns
{
get { return columns; }
}
public IDictionary<string, object> Attributes { get; set; }
public string EmptyText { get; set; }
public IGridSections<T> Sections
{
get { return sections; }
}
#endregion
private static Func<T, object> ReflectedPropertyValue(string
name)
{
return x => x.GetType().GetProperty(name).GetValue(x, new
object[] {});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment