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

ASP.Net RadGrid how to add multiple groupby programatically

2 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Roger
Top achievements
Rank 2
Iron
Iron
Iron
Roger asked on 25 Mar 2021, 07:45 PM

     I would like to add two levels of grouping in my grid.

This seems to work for 1st level of Grouping well, but now I want to add a sublevel similar to dragging a 2nd column up to grouping panel.  I have tried several iterations of the Select fields / GroupBy Expressions, but nothing seems to be working.  I have not seen a nice example of adding a 2nd level via code.

My 2nd level will be a field called TypeOfDoc.  I would like to show the count of documents in that header as well.

 

GridGroupByExpression expression = new GridGroupByExpression();
            // Select Fields
            GridGroupByField gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "ContentType";
            gridGroupByField.HeaderText = "Content Type ";
            gridGroupByField.HeaderValueSeparator = ";";
            expression.SelectFields.Add(gridGroupByField);

            // Adding Count Picking a field not in Group by
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "Title";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            gridGroupByField.FormatString = "<strong>({0})</strong>";
            gridGroupByField.Aggregate = GridAggregateFunction.Count;
            expression.SelectFields.Add(gridGroupByField);

            // GroupByField

            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "ContentType";
            gridGroupByField.HeaderText = "ContentType";
            expression.GroupByFields.Add(gridGroupByField);
            RadGrid1.MasterTableView.GroupByExpressions.Add(expression);

            RadGrid1.MasterTableView.GroupsDefaultExpanded = false;

2 Answers, 1 is accepted

Sort by
0
Roger
Top achievements
Rank 2
Iron
Iron
Iron
answered on 25 Mar 2021, 08:40 PM

After some Trial and Error effort I was able to get this to work and posting for others that may want to see a solution.  

* It does not give counts by Group, each count is of all records.  I am still working on this.

 

 GridGroupByExpression expression = new GridGroupByExpression();
            // Select Fields
            GridGroupByField gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "ContentType";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            expression.SelectFields.Add(gridGroupByField);

            // Adding Count Picking a field not in Group by
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "Title";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            gridGroupByField.FormatString = "<strong>({0})</strong>";
            gridGroupByField.Aggregate = GridAggregateFunction.Count;
            expression.SelectFields.Add(gridGroupByField);

            // GroupByField

            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "ContentType";
            gridGroupByField.HeaderText = " ";
            expression.GroupByFields.Add(gridGroupByField);
            RadGrid1.MasterTableView.GroupByExpressions.Add(expression);

            
            expression = new GridGroupByExpression();
            
            // Select Fields
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "TypeOfDocument";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            expression.SelectFields.Add(gridGroupByField);

            // Adding Count Picking a field not in Group by
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "Title";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            gridGroupByField.FormatString = "<strong>({0})</strong>";
            gridGroupByField.Aggregate = GridAggregateFunction.Count;
            expression.SelectFields.Add(gridGroupByField);

            // GroupByField

            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "TypeOfDocument";
            gridGroupByField.HeaderText = "";
            expression.GroupByFields.Add(gridGroupByField);
            RadGrid1.MasterTableView.GroupByExpressions.Add(expression);

            RadGrid1.MasterTableView.GroupsDefaultExpanded = false;

0
Doncho
Telerik team
answered on 30 Mar 2021, 01:01 PM

Hi Roger,

I am glad to hear you have found a solution to the issue. Thank you for sharing it with the community!

Concerning the Group aggregates, you may find it useful to check out the Group Footers article and review the implementation of our live demo - Group Footers.

I hope you will find this information helpful.

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Roger
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Roger
Top achievements
Rank 2
Iron
Iron
Iron
Doncho
Telerik team
Share this question
or