I have the grid which has groups set programmatically , and that functionality works as needed, subtotals get calculated fine. The grid is fully dynamic. What I need to do, is to have text inside each group footer which would display field name or field value of that group. That text needs to be under the column where group field name is displayed. For example:
ClientID: 1
ProductID: 2
Test 1 10 50
Test 2 20 60
ProductID 2 Total: 30 110
ClientID 1 Total: 30 110
Thanks,
Alex
15 Answers, 1 is accepted
Have you tried the Column property Aggregate to calculate the Sum? If so there is another property wich define the text.
Aggregate
="Sum" FooterText="My custom text"
/M
Unfortunately the RadGrid does not support the desired functionality. However it could be achieved with some custom logic. You could try getting all group header items and all group footer items into the RadGrid's PreRender event and change the footer text manually. For example:
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
GridItem[] headerItems = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader);
GridItem[] footerItems = RadGrid1.MasterTableView.GetItems(GridItemType.GroupFooter);
for
(
int
i = 0; i < headerItems.Length; i++)
{
var text = (headerItems[i]
as
GridGroupHeaderItem).Cells[1].Text;
(footerItems[i]
as
GridGroupFooterItem).Cells[3].Text = text +
" Total:"
;
for
(
int
j = 4; j < (footerItems[i]
as
GridGroupFooterItem).Cells.Count; j++)
{
var sumText = (footerItems[i]
as
GridGroupFooterItem).Cells[j].Text;
if
(sumText !=
" "
)
{
sumText = sumText.Split(
new
char
[] {
':'
}, StringSplitOptions.RemoveEmptyEntries)[1];
(footerItems[i]
as
GridGroupFooterItem).Cells[j].Text = sumText;
}
}
}
}
Also please note that this is not a supporter scenario.
Additionally I am sending you a simple example which demonstrates the mentioned approach above.
Greetings,
Radoslav
the Telerik team
This seems to work fine if I have one grouping, but if I am grouping on two or more columns it is not showing the footer text. I also want to show text the in the total grid footer.
I have tried the same code with grouping on multiple columns and it is working at my end. Can you please paste your code for further help?
The following code snippet shows how to add grid footer from code behind.
Vb.Net:
Private
total
As
Integer
= 0
Protected
Sub
RadGrid1_PreRender(sender
As
Object
, e
As
EventArgs)
For
Each
item
As
GridDataItem
In
RadGrid1.Items
total += Convert.ToInt32(item(
"total"
).Text)
Next
Dim
gridfooterItem
As
GridFooterItem =
DirectCast
(RadGrid1.MasterTableView.GetItems(GridItemType.Footer)(0), GridFooterItem)
gridfooterItem(
"total"
).Text =
"total is"
& total
'calculate total and display here
End
Sub
Thanks,
Princy.
I have aggregates already setup on certain columns (I added those in RadGrid1_ColumnCreated). That is working fine, so I do not need to do any sum, avg, etc. while doing the PreRender. I am strictly looking to show a label in the first cell of the footer that has the Grouping value. In Alex's example above, I just need to display Product 2 Total and Client ID 1 Total in the proper footers, so I don't even need the For j loop code.
Here is my code-behind:
Protected
Sub
RadGrid1_PreRender(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
RadGrid1.PreRender
Dim
headerItems
As
GridItem() = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)
Dim
footerItems
As
GridItem() = RadGrid1.MasterTableView.GetItems(GridItemType.GroupFooter)
Dim
GridFoot
As
GridItem() = RadGrid1.MasterTableView.GetItems(GridItemType.Footer)
TryCast(GridFoot(0), GridFooterItem).Cells(0).Text =
"<strong>Total</strong>"
For
i
As
Integer
= 0
To
headerItems.Length - 1
Dim
text = TryCast(headerItems(i), GridGroupHeaderItem).Cells(1).Text
TryCast(footerItems(i), GridGroupFooterItem).Cells(3).Text = text
For
j
As
Integer
= 4
To
TryCast(footerItems(i), GridGroupFooterItem).Cells.Count - 1
Dim
sumText = TryCast(footerItems(i), GridGroupFooterItem).Cells(j).Text
If
sumText <>
" "
Then
TryCast(footerItems(i), GridGroupFooterItem).Cells(j).Text = sumText
End
If
Next
Next
End
Sub
The problem arises in the line in BOLD above. footerItems(i) does not correspond to the appropriate footer for the header value the code is retrieving in the For loop. For example, Client ID 1 is headerItems(0) and Product ID 2 is headerItems(1). But when it comes to the footer for those headers, footeritems(0) is Product ID 2 and footeritems(1) is Client ID 1. It is reversed, but it gets even more complicated when you have 3 grouping columns and more than just 1 Product and 1 Client.
Here is the code from the ASPX page:
<
asp:TextBox
ID
=
"txtReportID"
runat
=
"server"
Visible
=
"false"
></
asp:TextBox
>
<
telerik:RadGrid
ShowGroupPanel
=
"true"
AutoGenerateColumns
=
"True"
ID
=
"RadGrid1"
AllowFilteringByColumn
=
"False"
AllowSorting
=
"True"
ShowFooter
=
"True"
runat
=
"server"
GridLines
=
"Both"
AllowPaging
=
"true"
EnableLinqExpressions
=
"false"
>
<
PagerStyle
Mode
=
"NextPrevAndNumeric"
/>
<
MasterTableView
ShowGroupFooter
=
"true"
AllowMultiColumnSorting
=
"True"
UseAllDataFields
=
"true"
>
</
MasterTableView
>
<
ClientSettings
AllowDragToGroup
=
"true"
/>
<
GroupingSettings
ShowUnGroupButton
=
"true"
/>
</
telerik:RadGrid
>
Like I said in my first post the solution provided in this case is not a supported scenario for the RadGrid and it is a custom solution. Also this code will work correctly only for RadGrid with one level of grouping. All other cases could not been handled appropriately because getting the cell which will be changed is hardcoded:
Dim
text = TryCast(headerItems(i), GridGroupHeaderItem).Cells(1).Text
TryCast(footerItems(i), GridGroupFooterItem).Cells(3).Text = text
In your case you need to find which is the changed cell into the footerItems.Cells collection and assign it a value based on the value in header's cell. Also please note that for the different levels of grouping the cells are different.
I hope this helps.
All the best,
Radoslav
the Telerik team
i have exactly the same issue "group footer text needs to be under group label all the way to the left, not where actual subtotal is".my grid is two level grouped and group footer of first level is not under the first level Groupheader.
is ther anyway to solve this?
thanks in advance,
Eva
The RadGrid has built-in Group Header and Footer Templates which can help you in your case to build a custom footer item with needed alignment. On the following online documentation article you can find more information:
http://www.telerik.com/help/aspnet-ajax/grid-group-header-footer-templates.html
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.
my Telerik version does not support Group Header and Footer Templates.
is there any other away?
Which version of our control do you use?
Also I found that you did not have any downloads in your account, may I ask you from where do you download the Telerik UI for ASP.NET AJAX dll?
Looking forward for your reply.
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.
i use version 2011.3.1115.35.
also i am a developer of a company that has your product so that's why you don't see any downloads from my account.
I suggest you to upgrade to the latest version of our controls. Since 2011 we have fixed huge number of bugs into the RadGrid and included some performance optimizations.
Additionally I am sending you a simple example which demonstrates the desired functionality without using the templates.
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.
Hi ,
I have a grid which is getting group by payment status(eg Paid ,Unpaid and Cancelled ).I am showing total of 2 columns i.e. amount and tax amount in the footer.
In the Footer, I want to add only the Sum of Paid and Unpaid not Cancelled one.
Please let me know if there is any to achieve this.
Thanks,
Sweta
For such custom calculation you should set "Custom" aggregate for the columns and handle the OnCustomAggregate event of the grid, where you need to manually calculate and set the total value. More information on this matter is available in the following articles:
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/aggregates
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/totals-in-grid-footers
Hope this helps.
Regards,
Konstantin Dikov
Telerik