How to autogenerate colums for New With {c, o.OrderId}

6 posts, 1 answers
  1. Peter Meinl
    Peter Meinl avatar
    35 posts
    Member since:
    Jan 2010

    Posted 21 Feb 2010 Link to this post

    When binding the GridView to a LINQ result specifying entity names ("c" in the example below) without attributes the GridView displays their type name only and does not autogenerate colummns for their attributes:

    Dim q = From c In qc Join s In qs On c.CustomerID Equals s.CustomerID  
     Select New With {c, s.Size}  
    DataGrid1.ItemsSource = q.ToList  
     
    does not autocreate the grid columns for "c", it displays the type name.  
     

    The grid displays:
    WpsApp1.NorthwindService.Customers | small
    WpsApp1.NorthwindService.Customers | big

    How can I make the GridView autogenerate columns for entities in "Select New {}"?

  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 24 Feb 2010 Link to this post

    Hello Peter Meinl,

    RadGridView can only generate columns for the immediate properties of the data items that are displayed. I guess that in your scenario you are getting two columns - one for the c in "{c, s.Size} "  and one for the Size property. At the moment columns for sub-properties can only be created manually. 


    Sincerely yours,
    Milan
    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Peter Meinl
    Peter Meinl avatar
    35 posts
    Member since:
    Jan 2010

    Posted 24 Feb 2010 Link to this post

    In "Select New With {c, s.Size}"
    "c" returns all properties of Customers.
    Analogous to a "select c.* "  in SQL.
  5. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 01 Mar 2010 Link to this post

    Hello Peter Meinl,

    c will indeed hold all properties of Customers but the anonymous class that is created will look something like this:

    public class AnonymousClass1
    {
        public c Customer
        {
            get;
            set;
        }
      
        public int Size
        {
            get;
            set;
        }
    }

    It will have only two properties and RadGridView will generate columns for those two properties. All Customers properties will be encapsulated within the c property. If you would like to expose other properties you could try something like:

    Select New With {c, s.Size, c.Property1, c.Property2}


    Sincerely yours,
    Milan
    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.
  6. Peter Meinl
    Peter Meinl avatar
    35 posts
    Member since:
    Jan 2010

    Posted 01 Mar 2010 Link to this post

    I am looking for a generic solution, where I do not have to specify each property.
    Analogous to the output from this SQL

    select

     

    c.*, o.OrderID from Customers c inner join Orders o on c.CustomerID = o.CustomerID

     

  7. Answer
    Milan
    Admin
    Milan avatar
    1989 posts

    Posted 04 Mar 2010 Link to this post

    Hello Peter Meinl,

    Well, that cannot be achieved automatically and there are quite a few possible solutions. One solution is to use the so called MicroModels which allows you to define dynamic properties on classes. It also allow you to automatically "replicate" properties from one object to another (AllProperties mthod). For example, you can create a simple class called BaseEntity with two properties Customer and Order and try something like this:

    Dim q = From c In qc Join s In qs On c.CustomerID Equals s.CustomerID  
     Select As New BaseEntity(c, s)

    Using MicroModel you can create a BaseEntityView which has all properties of c and some of s with just a few lines of code. And them use this BaseEntityView to bind the grid.

    Best wishes,
    Milan
    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready