This is a migrated thread and some comments may be shown as answers.

[Solved] Bind MVC-Grid to dynamic data

3 Answers 357 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ron
Top achievements
Rank 1
Ron asked on 04 Sep 2011, 11:10 PM
I have a project that I am converting an existing ASP.NET website to an ASP.NET MVC site.  This conversion is more for my own experience, but if I can accomplish this, the additional changes will be so much simpler.


Here is my scenario.  I have a stored procedure that returns a dynamically created dataset (columns returned are specific to the input parameters).  When I use ASP.NET and a Telerik RadGrid control, it binds perfectly.  However when I try to port this logic over to MVC I get multiple errors.

I initially attempted to bind the data is a similar way that I bind the data using ASP.NET.  The data I retrieve comes from a webmethod from a legacy ASP.NET 2.0 web service.  So due to that, I am stuck with dealing with the DataSet it returns.  

I have tried to return the first table in the DataSet (using the  AsEnumerable method) and then use the AutoGenerate method in the view to automatically generate the columns.

Controller's code
return View(ds.Tables[0].AsEnumerable());

View's Code
@{ Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(c => c.AutoGenerate(true))
    .Pageable()
    .Render();
}

This logic will return the grid, and will generate the pager correctly with the total number of rows and everything.  However, it shows no columns nor column headers.

When I change up the controller to add the following
private GridColumnSettings[] GetGridColumns(DataTable dataTable)
{
    GridColumnSettings[] gridColumnSettings = new GridColumnSettings[dataTable.Rows.Count];
 
    for (int i = 0; i < dataTable.Rows.Count;i++ )
    {
        gridColumnSettings[i] = new GridColumnSettings
        {
            Member = dataTable.Rows[i].Field<string>("DBFIELD"),
            Title = dataTable.Rows[i].Field<string>("CAPTION"),
            Format = (
                       (dataTable.Rows[i].Field<int>("NDATATYPE") == 4) ?
                           "{0:c}" :
                           (dataTable.Rows[i].Field<int>("NDATATYPE") == 5) ?
                               "{0:d}" :
                           "{0}"
                      ),
            Width = "20em",
        };
    }
    return gridColumnSettings;
}

and then add this to the view
.Columns(columns => columns.LoadSettings((IEnumerable<GridColumnSettings>)ViewData["Columns"]))

I get this error.
'object' does not contain a definition for 'Column0'

Is there a setting that I'm forgetting that will fix this issue?

Thanks.

Russ

3 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 05 Sep 2011, 10:05 AM
Hi Russell Smith,

 You can check this code library project which shows how to bind the Telerik MVC Grid to a datatable.

All the best,
Atanas Korchev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Ron
Top achievements
Rank 1
answered on 05 Sep 2011, 06:59 PM
Thank you.  that was the missing piece of the puzzle.
0
Gaurav
Top achievements
Rank 1
answered on 18 Jan 2013, 04:42 AM
Hi Atanas,

The code you have provided is really amazing. But we want batch editing in addition to that in the grid. We are really not able to implement that. Here is another thread I have started. http://www.telerik.com/community/forums/aspnet-mvc/grid/batch-editing-in-grid-with-dynamic-model.aspx

Can you please help implementing this.
Tags
Grid
Asked by
Ron
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Ron
Top achievements
Rank 1
Gaurav
Top achievements
Rank 1
Share this question
or