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:
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.
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
0
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
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
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
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
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:
code-behind:
All the best,
Rosen
the Telerik team
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:
but it doesn't work.
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.
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
Hello,
To achieve the desired functionality you need to add additional property into the datasource which returns the inner collection count. For example:
Then you can use this field as a sort expression:
Additionally I am sending you a simple example which demonstrates this approach.
I hope this helps.
Regards,
Radoslav
Telerik
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
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
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
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
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
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.