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

[Solved] Binding grid to an anonymous type

3 Answers 259 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.
Chris Williams
Top achievements
Rank 1
Chris Williams asked on 28 Nov 2009, 08:09 PM
Hi,

Do you have a brief example of binding the grid to an anonymous type?  I'm using OpenAccess ORM in a multi-tier environment, and have had nice results so far, because I've only had to return IQueryable of a strongly-typed item from the model. I have to return a slight variation now and want to do so via an anonymous type, but I haven't seen any example of how to do so.  I have the method set up in my DAL to return an object... just looking at how that might bind to the grid.

thanks

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 01 Dec 2009, 12:50 PM
Hello Chris,

In order to achieve your goal you will need to modify the source code. You will need to add extension method which will allow you bind grid to anonymous type. Here is a code snippet of the extension method:
namespace Telerik.Web.Mvc.UI
{
    public static class ViewComponentFactoryExtensions
    {
        /// <summary>
        /// Creates a new <see cref="Telerik.Web.UI.Grid<T>"/> bound to the specified data item type. Suitable for custom client-side binding where
        /// there is no server type corresponding to the data item.
        /// </summary>
        /// <example>
        /// <code lang="CS">
        ///  <%= Html.Telerik().GridWithAnonimousTypes(new {Name = "", Age = 0})
        ///             .Name("Grid")
        ///             .Columns(columns=>
        ///             {
        ///                 columns.Add(o => o.Name);
        ///                 columns.Add(o => o.Age);
        ///             })
        /// %>
        /// </code>
        /// </example>
        public static GridBuilder<T> GridWithAnonimousTypes<T>(this ViewComponentFactory factory, T value) where T : class
        {
            return factory.Grid<T>();
        }
    }
}

All the best,
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
Chris Williams
Top achievements
Rank 1
answered on 04 Dec 2009, 06:17 AM
Thanks, that's very helpful.

Chris
0
Box
Top achievements
Rank 1
answered on 15 Oct 2010, 09:42 AM
Georgi,

We've got a similar requirement.

The user selects a domain type (table name) from a drop down list and the grid needs to be populated with data from that table.

We're using OpenAccess ORM as well. We can invoke the method (using reflection) that returns the collection however, the collection type returned is of type Sytem.Object.

I created an extension as you've suggested but still get a cast compilation error: "Cannot implicitly convert GridBuilder<object> to object"

///Model
public class ExampleViewModel
{
     public object Items {get;set;}
  
     public void PopulateTableDetails(string tableName)
     {
            Type staticRepositoryType = typeof(IStaticRepository);
            MethodInfo[] sDRMethods = staticRepositoryType.GetMethods();
  
            tableType = Type.GetType("AssemblyName."+tableName+", AssemblyName");
  
            foreach (MethodInfo method in sDRMethods)
            {
                if (method.Name.Contains(domainTable))
                {
                    Items = (method.Invoke(_staticRepository, null));
                    //Items is a collection of "tableName" objects which has to be populated in the grid.
  
                }
            }
     }
}

//View
<%= Html.Telerik().GridWithAnonymousTypes(Model.Items) %> //does not compile. Method expecting GridBuilder<object> but Items is of type Object.


Your help is appreciated.

Tags
Grid
Asked by
Chris Williams
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Chris Williams
Top achievements
Rank 1
Box
Top achievements
Rank 1
Share this question
or