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

Grid Group by Expression Need row count with nested RadGrid

7 Answers 360 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 14 Dec 2012, 06:14 PM
Hi,

I have a radgrid with nested hierachal records, which are rows belonging to parent rows in the grid.

I was about to group the parent rows with Group by Expression with the following code

<telerik:GridGroupByExpression>
  <SelectFields>
    <telerik:GridGroupByField FieldAlias="Submitter" FieldName="SUBMITTER_NAME" />
  </SelectFields>
  <GroupByFields>
    <telerik:GridGroupByField FieldName="SUBMITTER_NAME" />
  </GroupByFields>
</telerik:GridGroupByExpression>

I would like to added code in the ascx file to add addition information about the number of records under the parent row before the user expanded the nested parent rows. It is help to see 0 child records or more child records so they know when to expand.

Like so: Client Name: Joe Bank, Inc,  Deposit Records: 3, Withdrawal Records: 6, Acounts : 1

With the group by exression, I am not sure about to add the aggregate code to make this happen.
Can this be done in the c# code to do a query for the record count and add the label so that the user can see the number of child records before expanding the parent row?

Any help is appreciate. 

 

7 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 19 Dec 2012, 02:07 PM
Hi John,

You can achieve the requested functionality on code-behind:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridGroupHeaderItem groupHeaderItem in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        if (!(groupHeaderItem.DataCell.Text.Contains("Child Items:")))
        {
            groupHeaderItem.DataCell.Text = groupHeaderItem.DataCell.Text +
           "; Child Items: " + groupHeaderItem.GetChildItems().Length.ToString();
        }
    }
}

An alternative approach would be to use group header template:
http://demos.telerik.com/aspnet-ajax/grid/examples/groupby/headerandfootertemplates/defaultcs.aspx

I hope this will prove helpful. Please give it a try and let me know about the result.

Greetings,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
nguyen
Top achievements
Rank 1
answered on 20 Sep 2013, 07:17 AM
dear Eyup,


I am using your code, and the result like
rawgroup.jpg

i want to count item parent group equal sum of all items of child group like groupping.jpg. How can i do like this??


please help

thank you!
                                              
0
Eyup
Telerik team
answered on 25 Sep 2013, 07:53 AM
Hi Nguyen,

You can use the following approach:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridGroupHeaderItem groupHeader in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        if (!(groupHeader.DataCell.Text.Contains("Child Items:")))
        {
            groupHeader.DataCell.Text = groupHeader.DataCell.Text +
           "; Child Items: " + GetAllChildItemsCount(groupHeader);
        }
    }
}
private int GetAllChildItemsCount(GridGroupHeaderItem parentGroupHeader)
{
    int count = 0;
    foreach (GridItem gridItem in parentGroupHeader.GetChildItems())
    {
        if (gridItem is GridDataItem)
        {
            count++;
        }
        else if (gridItem is GridGroupHeaderItem)
        {
            GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)gridItem;
            count += GetAllChildItemsCount(gridItem as GridGroupHeaderItem);
        }
    }
    return count;
}

Alternatively, you can use Template structure for your group headers and add count Aggregates:
http://demos.telerik.com/aspnet-ajax/grid/examples/groupby/headerandfootertemplates/defaultcs.aspx

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
nguyen
Top achievements
Rank 1
answered on 26 Sep 2013, 06:41 AM
yeah, it work.
Thanks so much! Eyup
0
Nick
Top achievements
Rank 1
answered on 19 Nov 2013, 09:23 PM
Thanks for the code as well.  Just what i was looking for!
0
Yulwenty
Top achievements
Rank 1
answered on 18 Dec 2013, 04:17 AM
Hello Telerik Admin, this is exactly what i need but this code only counting the child on the same page. When there is paging, the count will reset again. How to count the whole items for one group although there is paging?
Thank you
0
Eyup
Telerik team
answered on 23 Dec 2013, 07:07 AM
Hi Yulwenty,

In that case, you need to use GroupHeader aggregates. You can achieve this by using GroupHeaderTemplate:
http://www.telerik.com/help/aspnet-ajax/grid-group-header-footer-templates.html

Or setting Group aggregates declaratively or programmatically as demonstrated in the attached sample.

Please run the application and let me know if it helps you.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Eyup
Telerik team
nguyen
Top achievements
Rank 1
Nick
Top achievements
Rank 1
Yulwenty
Top achievements
Rank 1
Share this question
or