views:

63

answers:

1

Hi,

In My project i want to use the mvccontrib (http://mvccontrib.codeplex.com) to auto generate grid by passing the DataTable to the Grid. Currently Grid does not support this.

Grid takes IEnumerable(Of T) and auto generate columns. Is their any way i can achieve this by passing DataTable. Or How do i convert the DataTable to IEnumerable(Of T). My DataTable is completely Dynamic i don't know number of columns or names of the columns at design time. DataTable completely dynamic number of columns are not fixed it just any DataTable.

How can i achieve this?

In Short i want to convert Dynamic DataTable to IEnumerable(Of T). Or Is their any better way.

I tried following way

1) One way i tried is using .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 trid passing IEnumerable(Of SampleDynamicObject) but BuildColumns() method is not able to find out the proprieties dynamically added on to it.

2) I think Second way to modify the AutoGenerateColumns() method to work with DataTable, But it looks impossible as Grid Get initialize using IEnumerable(Of T) and all the call on grid for method is using IEnumerable(Of T). So it's come down to converting DataTable to IEnumerable(Of T) and my DataTable are dynamic i don't know their structure at design time So i can't write any specific class to create a object and convert DataTable to IEnumerable(Of T).

Does any one have any better way out on this.

Thanks

Sandy

A: 

If you're using .NET 3.5, you can use AsEnumerable (from DataTableExtensions. It would look something like this:

IEnumerable<DataRow> data = datatable.AsEnumerable();
JeffCren
Thanks Jeff, I already tried that My problem is mvccontrib Grid AutoGenerateColumns() Generate display a grid using IEnumerable(Of T) where T is Class and Hearder of the Grid are the Public Proprites of class. For Example if Employee class has LastName And FirstName as Prop. then Grid header will be Last Name and First Name. So i have to convert DataTable to Dynamic Class. and my datatable is dynamic
Sandy
I'm not sure I follow - you didn't say anything about Headers in your original question. So the issue is the Header names when you auto-generate the columns?
JeffCren
If you have the Properties of LastName and FirstName, how do you want the column headers displayed? Are you concatenating the two properties into one column?
JeffCren
mvccontrib Grid takes input as IEnumerable(Of T) where T is Class and Hearder of the Grid are the Public Proprites of class.This is how the AutoGenerateColumns() of grid works. But my datable is dynamic I don't know how many columns datable is going to have. It will be just any datatable. In short i have dynamic datatable from that i have to create dynamic class and dynamic properties and then create IEnumerable(Of T) for that class and pass it on to grid.
Sandy
Ah, I see what you mean now. I don't think there's any way to use AutoGenerateColumns if you're using a dynamic datatable, since you don't have any public properties to bind to for the column headers. You may be able to override the code in MVCContrib HTMLGridRenderer to do what you want.
JeffCren