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

How to bind a column to a child data object that is a list?

12 Answers 228 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 12 Aug 2010, 06:08 PM
I have a grid that binds a number of child data objects to columns with no issue, using the syntax defined at http://www.telerik.com/help/aspnet-ajax/grdbindingtosubobjects.html.

What I cannot figure out, however, is how to aggregate a child object that is a list or collection. For example, if I have a Customer object and I want to get the customer's first street address, I would use DataField="Customer.Addresses[0].Street" on a standard GridBoundColumn.

How can I get the count of the addresses? I have tried all sorts of GridCalculatedColumn DataFields and Expressions, to no avail. I am looking for something along the lines of this:
<telerik:GridCalculatedColumn DataFields="Customer.Addresses" 
Expression="Count( Child )" >

That doesn't work, of course. In fact, if I try to do a Count on any dotted data field, I get an exception of "System.Data.SyntaxErrorException: Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier." For example, just trying to use an expression of "Count({0})" with DataFields set to "Customer.FirstName" (of which there is only one), causes that exception to be thrown at runtime. Doing the same thing with a non-dotted data field, such as SendDate, does not cause the same exception.

12 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 18 Aug 2010, 07:54 AM
Hello Jonathan,

I am afraid that RadGrid does not support collections as DataFiled. The type that RadGrid binds must be  one of the following:
http://www.telerik.com/help/aspnet-ajax/grdbindablepropertytypes.html

Regards,
Nikolay
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
Jonathan
Top achievements
Rank 1
answered on 24 Aug 2010, 03:16 PM
Thanks for your reply. Does that mean it's not possible to do a Count off a child collection, as technically that data type would be an int?
0
Nikolay Rusev
Telerik team
answered on 27 Aug 2010, 07:47 AM
Hello Jonathan,

The result of Count will be int. However DataField is not one of the bindable types, it is collection which is not supported.

Regards,
Nikolay
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
Jonathan
Top achievements
Rank 1
answered on 31 Aug 2010, 11:22 PM
Any thoughts on what alternative method I might employ to get that information? Can I create a column that's not actually databound and manually set it's value in something like the Page_Load? If so, how?
0
Rosen
Telerik team
answered on 03 Sep 2010, 02:39 PM
Hi Jonathan,

You may consider using a TemplateColumn and call a custom function to retrieve and visualize the needed information from the current DataItem. Similar to the following:

 <telerik:GridTemplateColumn>
     <ItemTemplate>
         <%#CalculateAddressesCount((Customer)Container.DataItem)%>
     </ItemTemplate>
 </telerik:GridTemplateColumn>

code-behind:
protected string CalculateAddressesCount(Customer cust)
{
    return cust.Addresses.Count();
}

All the best,
Rosen
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
Daniel
Top achievements
Rank 1
answered on 30 Jul 2013, 11:30 AM
Hi,


And if I want to sort by that column, how can I do? I was thinking in something like:

SortExpression="<%#CalculateAddressesCount((Customer)Container.DataItem)%>"

but it doesn't work.

0
Mark
Top achievements
Rank 1
answered on 27 Oct 2014, 11:08 AM
I am in the same situation.
Using Model Binding i am trying to count one of the relationships.
This works fine in a template field to get the value like so.
                <telerik:GridTemplateColumn HeaderText="Total Members" >
                    <ItemTemplate>
                        <%# Item.Member.Count() %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

But i cant figure out how to get the sorting to work on this column.
0
Radoslav
Telerik team
answered on 29 Oct 2014, 12:20 PM
Hello,

To achieve the desired functionality you need to add additional property into the datasource which returns the inner collection count. For example:
public class Helper
{
    public int ID { get; set; }
    public string Item { get; set; }
    public List<Helper1> Member { get; set; }
    public int MembersCount
    {
        get
        {
            return Member.Count;
        }
    }
}
 
public class Helper1
{
    public int ID1 { get; set; }
}

Then you can use this field as a sort expression:
<telerik:GridTemplateColumn HeaderText="Total Members" SortExpression="MembersCount">
                    <ItemTemplate>
                        <%# Item.Member.Count() %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

Additionally I am sending you a simple example which demonstrates this approach.

I hope this helps.

Regards,
Radoslav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mark
Top achievements
Rank 1
answered on 07 Nov 2014, 12:36 PM
The issue is I am using entity framework.
So using the above method gives me the following error:
The specified type member "MembersCount" is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

any ideas?

Thanks
0
Radoslav
Telerik team
answered on 12 Nov 2014, 08:25 AM
Hi Matt,

In this case you need to add additional column into the database and re-generate the Entity Framework model. This column can be populated with a TRIGGER where when a row is added it can update the values. More information about triggers you can find here:
http://msdn.microsoft.com/en-us/library/ms189799.aspx

I hope this helps.

Regards,
Radoslav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Raph
Top achievements
Rank 1
answered on 20 Nov 2014, 06:05 AM
I want to add a custom property in radgrid SP webpart that lets me choose  the data source as another SP list. Is that customization possible?
0
Radoslav
Telerik team
answered on 21 Nov 2014, 09:32 AM
Hello Raph,

Please check the following forum thread which elaborate on the same matter:
http://www.telerik.com/forums/radgrid-from-sp2010-webpart-column-aggregates

I hope this helps.

Regards,
Radoslav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Jonathan
Top achievements
Rank 1
Rosen
Telerik team
Daniel
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Radoslav
Telerik team
Raph
Top achievements
Rank 1
Share this question
or