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

Generic column binding

7 Answers 101 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Fernando Real
Top achievements
Rank 1
Fernando Real asked on 03 Mar 2010, 07:02 PM
Hello all,

I'm trying to do something a bit different with your grid component. I'm trying to create a generic view for listing the content of the list.
Basically, I want to pass to the view an object list (List<object>), and I want to use reflection to determine which kind of objects are contained in my list, and then create a grid. The grid's collumns should be the properties of the object contained in the list. The objects in the list are all of same type. I'm having trouble to creating the collumns while iterating through the properties of the reflected object.

Can you please help me with this?

Thank you very much!

Fernando

7 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 04 Mar 2010, 08:16 AM
Hi Fernando Real,

Currently this is not an easy task because the bound columns need a c# Expression like this:
Expression<Func<TModel, TValue>>. Creating those from code requires quite a bit of reflection.

In the upcoming release we made an overload which accepts a string with the member name e.g.

columns.Add("OrderID") which is equivalent to columns.Add(o => o.OrderID)

That overload should make things easier.

However you still need to specify the type of your objects in the Grid. If it is not known until runtime you can use reflection (MakeGenericType).

Let me know if you are interested in trying the current internal build.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Kazi Manzur Rashid
Telerik team
answered on 04 Mar 2010, 08:41 AM
Hi Fernando Real,

We do have the support to show the Grid with data without defining the Columns, under the hood, uses similar reflection code to discover the columns, may be you can check the following example:

http://demos.telerik.com/aspnet-mvc/grid/autogeneratecolumns


All the best,
Kazi Manzur Rashid
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Fernando Real
Top achievements
Rank 1
answered on 04 Mar 2010, 10:48 AM
Hello all,

Thanks for your replies.

I'll start from the second reply, from Kazi. The suggestion you gave me would work ok if I have a strongly typified List. But my header is something like:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/DoubleContentArea.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<object>>" %>

As you can see, I'm sending an object list, so the autogenerated collumns will look for properties of object, which are none, and so I get a list with no collumns (although, the grid identifies the correct number of elements in the list, presenting me an empty grid but saying "Displaying items 1-<list size> of <list size>". So I guess that the approach of the autogenerated collumns will not work on my case.


The suggestion made by Atanas seems to suit my needs. The overload that accepts a string as member name seems great to me.
I didn't quite understood your comment: "However you still need to specify the type of your objects in the Grid. If it is not known until runtime you can use reflection (MakeGenericType)." Does that mean I'll have to specify something like Html.Telerik().Grid(List<MyReflectedType>) instead of just using Html.Telerik().Grid(Model) ? Can you clarify me on this issue?
The build you were talking about is something that is not yet prepared to be released? When will it be officially released? Will it still provide me the same stability?

Thanks in advance.

Best regards,

Fernando




0
Atanas Korchev
Telerik team
answered on 04 Mar 2010, 02:47 PM
Hello Fernando Real,

I investigated the problem further. Unfortunately even the overload which accepts string will not work in your case because it tries to access properties from the object type not the real type. I am afraid the only possible workaround (at least in theory) is to create the required expressions via code (with required cast from object to the real type). You can check the GridColumnGenerator class (from the source) for some hints about the implementation.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Emmanuel Morales
Top achievements
Rank 2
answered on 30 Mar 2010, 06:25 PM
How can I get access to the internal build that has the string parameter overload for column.add()?
0
Atanas Korchev
Telerik team
answered on 31 Mar 2010, 08:21 AM
Hi Emmanuel Morales,

It has been released officially in the Q1 2010 version. Take into consideration that the Add method has been obsoleted in favor of Bound though.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Rolf Toorn, van der
Top achievements
Rank 1
answered on 01 Apr 2010, 04:09 PM
Hi

How would you use MakeGenericType together with the grid?
My type is unknown at designtime. The application uses plug-ins.

thanks
Rolf
Tags
General Discussions
Asked by
Fernando Real
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Kazi Manzur Rashid
Telerik team
Fernando Real
Top achievements
Rank 1
Emmanuel Morales
Top achievements
Rank 2
Rolf Toorn, van der
Top achievements
Rank 1
Share this question
or