kala-tamin Auto generate grid using mvccontrib by passing datatable in ASP.net MVC 2 application
صفحه اصلي

Auto generate grid using mvccontrib by passing datatable in ASP.net MVC 2 application



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


How do I keep field values in the ASP.NET MVC form if it has been invalidated?

1:

Fill textboxes based upon current Dropdownlist (database items) selection (asp.NET MVC)
The question is quite old although maybe this could guidance any one else. Why does this code changes location as I need it toI just needed to fill grid with dynamic DataTable and I did it like this in view:. MVC .NET 4.0 : Image Links Index.cshtml. Are there any action filters in your project you feel are a must-have?
@using System.Data @using MvcContrib.UI.Grid @model SomeDataTable  @Html.Grid(Model.Rows.Cast<DataRow>()).Columns(columns =>{   foreach (var dataColumn in Model.Columns.Cast<DataColumn>()){     var column = dataColumn;     columns.For(x => x[column]).Named(column.Caption);   } }) 
New suggestions for error handling in ASP.NET MVC 2ASP.NET WebForms with MVC localization issue

2:

Is it possible to map complex types with SolrNet
If you're using .NET 3.5, you must use AsEnumerable (from DataTableExtensions. It would look any thing like this:.
IEnumerable<DataRow> data = datatable.AsEnumerable(); 

3:

It is possible by creating a custom GridModel. Here I based my grid on an IList filled with IDictionaries, although this should also be possible with data taken from a DataTable. The trick is to commit the data structure to the constructor of the custom GridModel and let the custom GridModel generate columns based on the data structure. It is a bit ugly, although it works good. (and it is even uglier in VB.NET with the funny lambda expressions). The controller action code:.
Dim list As IList(Of IDictionary(Of String, String)) = New List(Of IDictionary(Of String, String)) Dim row1 As IDictionary(Of String, String) = New Dictionary(Of String, String)() row1.Add("Column1", "ValueColumn1Row1") row1.Add("Column2", "ValueColumn2Row1") Dim row2 As IDictionary(Of String, String) = New Dictionary(Of String, String)() row2.Add("Column1", "ValueColumn1Row2") row2.Add("Column2", "ValueColumn2Row2") list.Add(row1) list.Add(row2) ViewData("DynamicData") = list 
The custom GridModel:.
Public Class DynamicGridModel Inherits MvcContrib.UI.Grid.GridModel(Of IDictionary(Of String, String))      Public Sub New(ByVal dataToRender As IList(Of IDictionary(Of String, String)))         If dataToRender.Count > 0 Then             For Each name As String In dataToRender(0).Keys                 Dim columnName As String = name                 Column.For(Function(rowData As IDictionary(Of String, String)) rowData(columnName)).Named(columnName)             Next         End If     End Sub  End Class 
The syntax in the view:.
Html.Grid(Of IDictionary(Of String, String))("DynamicData").WithModel(New DynamicGridModel(ViewData("DynamicData"))) 


40 out of 100 based on 40 user ratings 120 reviews