I have an MVC application where I need to be able to display various records in a view. For this, I went the route of using the built in Webgrid control. The problem I faced was taking my data from my Datatable returned from the database and converting it into an IEnumerable for the grid. I came across a method on SO to convert it to a type of List Dynamic which seemed to work well enough, but encountered issues when displaying more than about 6 columns of data:
public static dynamic serializeToDynamic(DataTable dt)
{
var result = new List<dynamic>();
foreach (System.Data.DataRow row in dt.Rows)
{
var obj = (IDictionary<string, object>)new System.Dynamic.ExpandoObject();
foreach (System.Data.DataColumn col in dt.Columns)
{
obj.Add(col.ColumnName, row[col.ColumnName]);
}
result.Add(obj);
}
return result;
}
I have a grid that needs to display 28 columns and using this method is extremely slow, with the page taking close to a minute to load. I was unsuccessful in finding any alternatives, so I went ahead and made a model for this information and bound the DataTable to this model which proved to be much faster loading in about 2 seconds. I'd rather not have to use strongly typed models just to display this data, my question is is there any other method to convert a DataTable to work with a webgrid?