Grid Group by Expression Need row count with nested RadGrid

8 posts, 0 answers
  1. John
    John avatar
    8 posts
    Member since:
    Nov 2012

    Posted 14 Dec 2012 Link to this post

    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. 

     

  2. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 19 Dec 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. nguyen
    nguyen avatar
    19 posts
    Member since:
    Aug 2012

    Posted 20 Sep 2013 Link to this post

    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!
                                                  
  5. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 25 Sep 2013 Link to this post

    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.
  6. nguyen
    nguyen avatar
    19 posts
    Member since:
    Aug 2012

    Posted 26 Sep 2013 Link to this post

    yeah, it work.
    Thanks so much! Eyup
  7. Nick
    Nick avatar
    1 posts
    Member since:
    Aug 2013

    Posted 19 Nov 2013 Link to this post

    Thanks for the code as well.  Just what i was looking for!
  8. Yulwenty
    Yulwenty avatar
    1 posts
    Member since:
    Dec 2013

    Posted 17 Dec 2013 Link to this post

    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
  9. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 23 Dec 2013 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017