return datatable and bind to Grid.Mvc

Jul 22, 2014 at 12:28 PM
Edited Jul 22, 2014 at 12:29 PM
Hello everyone

I have a procedure which returns datatable object and my controller gets this object. thus far everything is okey but I have no idea how can i bind to Grid.Mvc this data ?
Coordinator
Jul 28, 2014 at 4:37 AM
Hi,

You need to convert your datatable object to strongly typed collection like this:
public class MyGridItem
{
    public string Title { get; set; }
    public int Count { get; set; }

    static MyGridItem CreateFromRow(DataRow row)
    {
        var item = new MyGridItem();
        item.Title = row["Title"].ToString();
        item.Count = (int)row["Count"];
        // other fields
        return item;
    }

    static IEnumerable<MyGridItem> CreateFromTable(DataTable table)
    {
        var list = new List<MyGridItem>();
        foreach (DataRow row in table.Rows)
        {
            list.Add(CreateFromRow(row));
        }
        return list;
    }
}
In controller:
var items = MyGridItem.CreateFromTable(table)
return View(items);
Mar 20, 2015 at 3:24 PM
Hello,

and what can I do if the DataTable is dynamic? I want to use mvc.grid to populate from a generic datatable that is created at runtime?

Thanks!