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

Programatically creating a MVC grid

6 Answers 138 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.
Rahul Agashe
Top achievements
Rank 1
Rahul Agashe asked on 30 Dec 2009, 07:21 AM
Hi,

I want to create a grid programatically in ASP.Net MVC, depending on the model and its values.

I looked through the samples, but could not see the one.

Can some one know me whether is it possible and if yes how?

Thanks,

Rahul Agashe

6 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 30 Dec 2009, 11:11 PM
Hi Rahul,

In order to achieve your goal you will need to use auto generated columns. Please review this online demo for more information.

Sincerely yours,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rahul Agashe
Top achievements
Rank 1
answered on 31 Dec 2009, 07:35 AM
No what I mean by creating a Grid programatically, is that all the details about the columns such as header, sortable etc are provided by the Model it self and I would like to add columns as below,

.Columns(columns =>

{

 

foreach (TelerikEvaluation.Customer lstRows in Model.Records)

 

{

 

int RowCount = 0;

 

columns.Add(c => c.EmpId).Title(lstRows.EmpId.ToString()).Filterable(

false).Sortable(false);

 

columns.Add(c => c.FirstName).Filterable(

false).Sortable(false);

 

RowCount++;

 

break;

 

}

})

Something like a wrapper over Telerik grid, which will accept a generic Type Model and render a grid as per the model.

0
Atanas Korchev
Telerik team
answered on 31 Dec 2009, 10:34 AM
Hello Rahul Agashe,

I think that this is possible right now. Are you experiencing any difficulties creating columns programmatically?

Greetings,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rahul Agashe
Top achievements
Rank 1
answered on 31 Dec 2009, 11:45 AM

 

I am trying to add columns using below code,

foreach
(TelerikEvaluation.MetaDataInfo MetaDataRow in Model.MetaDataData)

 

{

columns.Add(c => c.GetType().GetProperty(MetaDataRow.PropertyName).GetValue(c,

null)).Title(MetaDataRow.PropertyHeader);

 

}

The problem is that Grid is displaying the Headers correctly, but the Rows render displayes data only from the Last Column in the For loop.

If I write Code to add multiple columns as below, grid is displaying records correctly,

columns.Add(c => c.EmpId);

 

 

 

columns.Add(c => c.FirstName);

 

 

 

columns.Add(c => c.LastName);

 

 

 

columns.Add(c => c.BirthDate);

 

 

 

Please see attached screen shot for clerification. Is it a bug or I need to change some of my code?

0
Atanas Korchev
Telerik team
answered on 31 Dec 2009, 04:20 PM
Hi Rahul Agashe,

Unfortunately the grid does not support the following syntax for defining the column binding:

columns.Add(c => c.GetType().GetProperty(MetaDataRow.PropertyName).GetValue(c,

null))


While it is valid C# and may work on initial binding it would fail if you try to sort or page. The reason is that the MVC grid understand only simple C# expressions - only member access.

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Emmanuel Morales
Top achievements
Rank 2
answered on 30 Mar 2010, 05:23 PM
I see this was posted a few months back. Is this still an issue? Is there a way to create the columns dynamically now?
Tags
Grid
Asked by
Rahul Agashe
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Rahul Agashe
Top achievements
Rank 1
Atanas Korchev
Telerik team
Emmanuel Morales
Top achievements
Rank 2
Share this question
or