MVCContrib and Datatables

125 views
Skip to first unread message

Mike

unread,
Jul 6, 2010, 10:49:11 AM7/6/10
to mvccontrib-discuss
Hello:

I am trying to fetch some solutions to my problem here:

Anyone out there with a useful answer, please help out?

I want to use the mvccontrib (http://mvccontrib.codeplex.com) to auto
generate grid by passing the DataTable to the Grid. At the moment,the
Grid does not support this.

Grid takes in IEnumerable(Of T) and auto generate columns. Is there
some way I could achieve this by say, passing DataTable. Or How do I
convert the DataTable to IEnumerable(Of T).
The DataTable is completely Dynamic and I wouldnt be able to tell the
number of columns or names of the columns. Since the DataTable is very
dynamic , hence the number of columns are not fixed so its just about
any DataTable.

How do I go about this ?

I want to convert a Dynamic DataTable to IEnumerable(Of T). Or Is
their any other better way ???

I have tried the following approaches but couldnt go any far :

1) One approach was to use .net 4.0 Dynamic, DynamicObject and
ExpandoObject I created a dynamic class as shown here
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trygetindex.aspx.
I have tried to pass IEnumerable(Of SampleDynamicObject) but the
BuildColumns() method is not able to find out the propreties
dynamically added on to it.

2) Second approach to modifing the AutoGenerateColumns() method to
work with DataTable, But it looks impossible since a Grid Get
initialize using IEnumerable(Of T) and all the calls on grid for
method uses IEnumerable(Of T). So talking about converting DataTable
to IEnumerable(Of T) and the DataTable being dynamic, i don't know the
structure at design time at present, So its probably not possible to
write any specific class to create a object and convert DataTable to
IEnumerable(Of T).

Jeremy Skinner

unread,
Jul 6, 2010, 11:13:43 AM7/6/10
to mvccontri...@googlegroups.com
The grid is not designed to work directly with datatables - you will need to project the datatable into a collection of strongly typed objects before using it with the grid.

Jeremy


--
Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go to http://mvccontrib.org

To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

John Teague

unread,
Jul 6, 2010, 11:23:36 AM7/6/10
to mvccontri...@googlegroups.com
There are some extension methods to convert a datatable to IEnumerable, but don't remember them offhand.

jigne...@gmail.com

unread,
Mar 27, 2014, 5:14:21 AM3/27/14
to mvccontri...@googlegroups.com
Hi,
 
Try
 
Controller code
public ActionResult Index()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Product Price", typeof(double));
dt.Rows.Add(new object[] { 1, "Test", 12.34 });
dt.Rows.Add(new object[] { 2, "Test 232", 12.34 });
dt.Rows.Add(new object[] { 3, "Test 232", 15.34 });
dt.Rows.Add(new object[] { 4, "Testsdas", 16.34 });
dt.Rows.Add(new object[] { 5, "Test asas", 17.34 });
dt.Rows.Add(new object[] { 6, "Test asas", 18.34 });
dt.Rows.Add(new object[] { 7, "Test asas", 19.34 });

ViewBag.Message = "Welcome to ASP.NET MVC!";
TempData["test"] = "session less controller test";
return View(dt);
}
 
View code
 
@model System.Data.DataTable
@using MvcContrib.UI.Grid;
@using System.Data;
@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
        http://asp.net/mvc</a>.
</p>
<div>
    @{
        int count = Model.Columns.Count - 1;
        @Html.Grid(Model.AsEnumerable()).Columns(column =>
   {
       for (var i = 0; i <= count; i++)
       {
           var column1 = Model.Columns[i];
           column.For(p => p[column1]).Named(column1.Caption);
       }
   })
    }
</div>
 
hope this will help you.
 

 

 

 

 

 

 

Reply all
Reply to author
Forward
0 new messages