Total records in Grouped Columns

In Drag column, for first column it is showing total records in that group, but when adding one more field, it is showing no. of total sub group records, and for sub group field it is showing total records.. For first field how to get the total records only.

Please check the screenshot in attachments
Thanks
Deepak

Attachment: SFDATAGRIDdrag_summary_615bf17.rar

15 Replies

AK Adhikesevan Kothandaraman Syncfusion Team August 13, 2018 11:49 AM UTC

Hi Deepak, 

Thanks for using Syncfusion products. 

You can achieve your required scenario by adding the CaptionSummaryRow for the SfDataGrid. The following code sample shows how to add the count aggregate for the caption summary to display the total items count in the caption summary text, 

Code Sample: 
sfDataGrid.CaptionSummaryRow = new GridSummaryRow() 
{ 
    Name = "CaptionSummary", 
    ShowSummaryInRow = true, 
    Title = "Total count of {Key} : {CaptionSummaryColumn}", 
    SummaryColumns = new ObservableCollection<ISummaryColumn>() 
    { 
        new GridSummaryColumn()  
        {  
            MappingName = "OrderID",                          
            Format="{Count}" 
            SummaryType= SummaryType.CountAggregate,  
            Name = "CaptionSummaryColumn" 
        } 
    }, 
}; 

 

Sample Link: 

Regards, 
Adhi 



DE Deepak September 8, 2018 08:43 AM UTC

Hi,

as per your suggestion, trying the same but having issues.

1. As per your suggestion, you have given particular fileds (hardcoded code), where as I want the same for all fields if they are selected in grouping. We are using dynamic input for grid. As per your suggestion, codes have been added but when we are trying to group it goes to main function  static void main()  in line Application.Run(new Form1()); and showing error message ColumnName cannot be NULL.

2. This was default command given.   

  this.sfDataGrid1.GroupCaptionTextFormat = "{Key} : {ItemsCount}";

If Only one Field is selected in drop group area, then Sum records is showing properly, but if additional filed is added in drag group area as sub group then for main group total records count was change to total sub group counts.. I want result as per below :

A group : 25 Records (2 Sub Group)
1 Sub Group : 15 records
Name 1
Name 2
Name 3
Name 4
Name 5
Name 6
Name 7
Name 8
Name 9
Name 10
Name 11
Name 12
Name 13
Name 14
Name 15

2 Sub Group : 10 Records
Name 21
Name 22
Name 23
Name 24
Name 25
Name 26
Name 27
Name 28
Name 29
Name 30


If Sub Group is there, in that case it has to show total sum Counts of records as well as total sub groups count. Both the result is required.

Please reply at the earliest.
Thanks
Deepak



AA Arulraj A Syncfusion Team September 10, 2018 12:43 PM UTC

Hi Deepak, 

Thanks for your update. 

Query  
Response 
As per your suggestion, you have given particular fields (hardcoded code), whereas I want the same for all fields if they are selected in grouping. We are using dynamic input for grid. As per your suggestion, codes have been added but when we are trying to group it goes to main function  static void main()  in line Application.Run(new Form1()); and showing error messageColumnName cannot be NULL. 
“The ColumnName cannot be null” exception will be occurred only on the mapping name is not exist while grouping the column with the CaptionSummary row. Since it has been thrown intentionally,  the given column name is needs to be exist in the data source of the SfDataGrid. 

If Only one Field is selected in drop group area, then Sum records is showing properly, but if additional filed is added in drag group area as sub group then for main group total records count was change to total sub group counts.. I want result as per below : 
 
A group : 25 Records (2 Sub Group) 
1 Sub Group : 15 records 
Name 1 
Name 2 
Name 3 
Name 4 
Name 5 
Name 6 
Name 7 
Name 8 
Name 9 
Name 10 
Name 11 
Name 12 
Name 13 
Name 14 
Name 15 
 
2 Sub Group : 10 Records 
Name 21 
Name 22 
Name 23 
Name 24 
Name 25 
Name 26 
Name 27 
Name 28 
Name 29 
Name 30 
 
You can achieve your required scenario by using the DrawCell event. In this event, you can set the display text to draw on the caption summary row. Refer to the following code sample, 

void sfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.CaptionCoveredRow && !string.IsNullOrEmpty(e.DisplayText)) 
    { 
        var displayText = ""; 
        var group = (e.DataRow.RowData as Group); 
        if (group != null) 
        { 
            if (group.Groups != null) 
            { 
                int records = 0; 
                foreach (var g in group.Groups) 
                { 
                    if (g.Records != null) 
                        records += g.Records.Count; 
                } 
 
                displayText += group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
            } 
            else if (group.Records != null) 
                displayText = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
            e.DisplayText = displayText; 
        } 
    } 
} 

 
 
Arulraj A 



DE Deepak September 10, 2018 01:02 PM UTC

pls send me sample for the same as getting error message for Field Group

the type or namespace name 'Group' could not be found.....

var group = (e.DataRow.RowData as Group);




AA Arulraj A Syncfusion Team September 11, 2018 06:12 AM UTC

Hi Deepak, 

Thanks for your update. 

The type Group will be found in the Syncfusion.Data namespace. Please make use of the following code and sample from the given location. 

Code Example :  

void sfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.CaptionCoveredRow && !string.IsNullOrEmpty(e.DisplayText)) 
    { 
        var displayText = ""; 
        var group = (e.DataRow.RowData as Syncfusion.Data.Group); 
        if (group != null) 
        { 
            if (group.Groups != null) 
            { 
                int records = 0; 
                foreach (var g in group.Groups) 
                { 
                    if (g.Records != null) 
                        records += g.Records.Count; 
                } 
 
                displayText += group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
            } 
            else if (group.Records != null) 
                displayText = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
            e.DisplayText = displayText; 
        } 
    }  
} 


Arulraj A 



DE Deepak September 11, 2018 08:02 AM UTC

Hi Arulraj A,

Thanks for your reply.

As per your Sample, When First Field Customer_ID is selected as Drag a Column, It is showing with Records. Again Product_name also Drag into Column, and now showing Customer total Records with sub groups, but after this again one field is selected shipping_address then Customer_ID record is showing as 0 records.

please note that there must be no change in Total Records, if added more sub groups also

Please Revert back ASAP.

Thanks
Deepak

Attachment: ERROR1_2deeb4b3.zip


MA Mohanram Anbukkarasu Syncfusion Team September 12, 2018 09:26 AM UTC

Hi Deepak, 

Thanks for your update.  

You can make use of the following code to achieve your requirement. Please refer to the following code example and sample from the given location. 

Code Example : 
sfDataGrid.DrawCell += sfDataGrid_DrawCell; 
 
void sfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.CaptionCoveredRow && !string.IsNullOrEmpty(e.DisplayText)) 
    { 
        var displayText = ""; 
        var group = (e.DataRow.RowData as Syncfusion.Data.Group); 
        if (group != null) 
        { 
            if (group.Groups != null) 
            { 
                int records = 0; 
                records = GetTotalRecordsCount(group); 
                displayText += group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
            } 
            else if (group.Records != null) 
                displayText = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
            e.DisplayText = displayText; 
        } 
    } 
} 
 
private int GetTotalRecordsCount(Group group) 
{ 
    int count = 0; 
 
    if (group.Groups != null) 
    { 
        foreach (var g in group.Groups) 
        { 
            if (g.Groups != null) 
                foreach (var g1 in g.Groups) 
                    count += GetTotalRecordsCount(g1); 
 
            if (g.Records != null) 
                count += g.Records.Count; 
        } 
    } 
    else 
        if (group.Records != null) 
            count += group.Records.Count; 
 
    return count; 
} 
 
 


 


Regards, 
Mohanram A. 



DE Deepak September 12, 2018 09:51 AM UTC

Hi Mohanram A,

Thanks for your reply.

Its Working fine, but now this result unable to export in Excel and export in PDF

While exporting to excel and exporting to pdf, the same result is not getting.

Pls update the same in sample and send me the same

Thanks
Deepak


MA Mohanram Anbukkarasu Syncfusion Team September 14, 2018 12:44 PM UTC

Hi Deepak, 

Thanks for your update. 

Your requirement can be achieved by creating a custom DataGridToPdfConverter and override the ExportGroupCaptionToPdf method. Please refer to the following code example and sample from the given location. 

Code Example : 
public class DataGridToPdfConverterExt : DataGridToPdfConverter 
{ 
    protected override void ExportGroupCaptionToPdf(Syncfusion.WinForms.DataGrid.SfDataGrid sfgrid, Syncfusion.Pdf.Grid.PdfGrid pdfGrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions, Group group) 
    { 
        var startColIndex = 0; 
        //Add a new row for caption summary 
        var row = pdfGrid.Rows.Add(); 
 
        if (!pdfExportingOptions.AutoRowHeight) 
            row.Height = (float)(sfgrid.RowHeight / 1.4); 
        var pdfColIndex = startColIndex + group.Level - 1; 
 
        //set the border and column span for indent cells 
        if (pdfColIndex != startColIndex) 
        { 
            row.Cells[startColIndex].Style = new PdfGridCellStyle { Borders = { All = new PdfPen(PdfBrushes.Transparent, 0f) } }; 
            row.Cells[startColIndex].ColumnSpan = pdfColIndex; 
        } 
 
        string summaryDisplayTextForRow = string.Empty; 
        PdfGridCell pdfGridCell = null; 
 
        if (sfgrid.CaptionSummaryRow == null) 
        { 
            var propertyName = (view.GroupDescriptions[group.Level - 1] as PropertyGroupDescription).PropertyName; 
            var groupedColumn = sfgrid.Columns.FirstOrDefault(col => col.MappingName == propertyName); 
            var columnHeaderText = (groupedColumn != null) ? groupedColumn.HeaderText : propertyName; 
 
 
            if (group != null) 
            { 
                if (group.Groups != null) 
                { 
                    int records = 0; 
                    records = GetTotalRecordsCount(group); 
                    summaryDisplayTextForRow = group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
                } 
                else if (group.Records != null) 
                    summaryDisplayTextForRow = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
            } 
 
            pdfGridCell = row.Cells[pdfColIndex]; 
 
            PdfGridCellStyle captionCellStyle = new PdfGridCellStyle(); 
            captionCellStyle.Borders.All = new PdfPen(PdfBrushes.DarkGray, 0.2f); 
            var font = new Font("Segoe UI", 9.5f, System.Drawing.FontStyle.Regular); 
            captionCellStyle.Font = new PdfTrueTypeFont(font, true); 
            pdfGridCell.Style = captionCellStyle; 
            pdfGridCell.ColumnSpan = pdfGrid.Columns.Count - pdfColIndex; 
 
            ExportSummaryCellToPdf(sfgrid, pdfGridCell, pdfExportingOptions, ExportCellType.GroupCaptionCell, summaryDisplayTextForRow, 
                                              sfgrid.CaptionSummaryRow, string.Empty); 
        } 
    } 
 
    private int GetTotalRecordsCount(Group group) 
    { 
        int count = 0; 
 
        if (group.Groups != null) 
        { 
            foreach (var g in group.Groups) 
            { 
                if (g.Groups != null) 
                    foreach (var g1 in g.Groups) 
                        count += GetTotalRecordsCount(g1); 
 
                if (g.Records != null) 
                    count += g.Records.Count; 
            } 
        } 
        else 
            if (group.Records != null) 
                count += group.Records.Count; 
 
        return count; 
    } 
} 


Please make use of the below code to export grid to pdf. 

Code  
var dataGridToPdfConverter = new DataGridToPdfConverterExt(); 
var pdfGrid = dataGridToPdfConverter.ExportToPdfGrid(sfDataGrid, sfDataGrid.View, options); 
pdfGrid.Draw(page, new PointF()); 


For excel exporting you have to create DataGridToExcelConverter same as pdf. Please refer to the sample from  the following location. 
 

Regards, 
Mohanram A. 



DE Deepak September 23, 2018 11:53 AM UTC

Hi Mohanram A.,

Thanks for your reply.

After using datagridtoexcelconver excel engine,now facing two problems.

1. Summary Background Color changed. How I can set background colour of Summary as blue color

2. Summary fonts and size change, how to make it verdana fonts and size 10

Pls reply at the earliest.

Thanks
Deepak





AA Arulraj A Syncfusion Team September 24, 2018 09:23 AM UTC

Hi Deepak, 

Thanks for your update. 

You can use any of the following solutions to change the appearance of the caption summary in the exported excel sheet. 

Solution 1: 

You can export the DataGrid to excel with style. For that you need to include the following highlighted code into the ExportGroupCaptionToExcel method of the custom DataGridToExcelConverter.  

Code : 
protected override void ExportGroupCaptionToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Group group) 
{ 
 
    var excelRowIndex = (int)this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
    var excelColumnIndex = (int)this.GetType().BaseType.GetField("excelColumnIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
 
    //Below code merges All the GroupCaptionCells, if ShowSummaryInRow is True. 
    if ((grid.CaptionSummaryRow == null) || grid.CaptionSummaryRow.ShowSummaryInRow) 
    { 
        sheet.MigrantRange[excelRowIndex, 1, excelRowIndex, (1 + excelExportingOptions.Columns.Count) - 1].Merge(); 
    } 
 
    IStyle groupCaptionStyle; 
 
    if (sheet.Workbook.Styles.Contains("DataGridGroupCaptionCell")) 
        groupCaptionStyle = sheet.Workbook.Styles["DataGridGroupCaptionCell"]; 
    else 
        groupCaptionStyle = sheet.Workbook.Styles.Add("DataGridGroupCaptionCell"); 
 
    groupCaptionStyle.Color = Color.Blue; 
    groupCaptionStyle.Font.FontName = "Verdana"; 
    groupCaptionStyle.Font.Size = 10; 
 
    var summaryDisplayTextForRow = string.Empty; 
 
    if (group != null) 
    { 
        if (group.Groups != null) 
        { 
            int records = 0; 
            records = GetTotalRecordsCount(group); 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
        } 
        else if (group.Records != null) 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
    } 
 
    IRange summaryRange = null; 
    summaryRange = sheet.MigrantRange[excelRowIndex, excelExportingOptions.StartColumnIndex]; 
    ExportRowStyle(summaryRange, groupCaptionStyle, excelExportingOptions.RowIndex); 
    ExportSummaryCellToExcel(grid, summaryRange, ExportCellType.GroupCaptionCell, summaryDisplayTextForRow, null, string.Empty, excelExportingOptions); 
    if (excelExportingOptions.ExportStyle) 
        ExportGridStylesToExcel(grid.Style.CaptionSummaryRowStyle, summaryRange, excelExportingOptions); 
    excelRowIndex++; 
    this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, excelRowIndex); 
} 


Solution 2: 
You can change the back color and font of the caption summary in the exported excel sheet directly by using the following code.  

Code : 
protected override void ExportGroupCaptionToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Group group) 
{ 
 
    var excelRowIndex = (int)this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
    var excelColumnIndex = (int)this.GetType().BaseType.GetField("excelColumnIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
 
    //Below code merges All the GroupCaptionCells, if ShowSummaryInRow is True. 
    if ((grid.CaptionSummaryRow == null) || grid.CaptionSummaryRow.ShowSummaryInRow) 
    { 
        sheet.MigrantRange[excelRowIndex, 1, excelRowIndex, (1 + excelExportingOptions.Columns.Count) - 1].Merge(); 
    } 
 
    IStyle groupCaptionStyle; 
 
    if (sheet.Workbook.Styles.Contains("DataGridGroupCaptionCell")) 
        groupCaptionStyle = sheet.Workbook.Styles["DataGridGroupCaptionCell"]; 
    else 
        groupCaptionStyle = sheet.Workbook.Styles.Add("DataGridGroupCaptionCell"); 
 
    groupCaptionStyle.Color = Color.Blue; 
    groupCaptionStyle.Font.FontName = "Verdana"; 
    groupCaptionStyle.Font.Size = 10; 
 
    var summaryDisplayTextForRow = string.Empty; 
 
    if (group != null) 
    { 
        if (group.Groups != null) 
        { 
            int records = 0; 
            records = GetTotalRecordsCount(group); 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
        } 
        else if (group.Records != null) 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
    } 
 
    IRange summaryRange = null; 
    summaryRange = sheet.MigrantRange[excelRowIndex, excelExportingOptions.StartColumnIndex]; 
    ExportRowStyle(summaryRange, groupCaptionStyle, excelExportingOptions.RowIndex); 
    ExportSummaryCellToExcel(grid, summaryRange, ExportCellType.GroupCaptionCell, summaryDisplayTextForRow, null, string.Empty, excelExportingOptions); 
    excelRowIndex++; 
    this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, excelRowIndex); 
} 

 
Regards, 
Arulraj A 



DE Deepak replied to Arulraj A March 16, 2019 02:47 PM UTC

Hi Deepak, 

Thanks for your update. 

You can use any of the following solutions to change the appearance of the caption summary in the exported excel sheet. 

Solution 1: 

You can export the DataGrid to excel with style. For that you need to include the following highlighted code into the ExportGroupCaptionToExcel method of the custom DataGridToExcelConverter.  

Code : 
protected override void ExportGroupCaptionToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Group group) 
{ 
 
    var excelRowIndex = (int)this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
    var excelColumnIndex = (int)this.GetType().BaseType.GetField("excelColumnIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
 
    //Below code merges All the GroupCaptionCells, if ShowSummaryInRow is True. 
    if ((grid.CaptionSummaryRow == null) || grid.CaptionSummaryRow.ShowSummaryInRow) 
    { 
        sheet.MigrantRange[excelRowIndex, 1, excelRowIndex, (1 + excelExportingOptions.Columns.Count) - 1].Merge(); 
    } 
 
    IStyle groupCaptionStyle; 
 
    if (sheet.Workbook.Styles.Contains("DataGridGroupCaptionCell")) 
        groupCaptionStyle = sheet.Workbook.Styles["DataGridGroupCaptionCell"]; 
    else 
        groupCaptionStyle = sheet.Workbook.Styles.Add("DataGridGroupCaptionCell"); 
 
    groupCaptionStyle.Color = Color.Blue; 
    groupCaptionStyle.Font.FontName = "Verdana"; 
    groupCaptionStyle.Font.Size = 10; 
 
    var summaryDisplayTextForRow = string.Empty; 
 
    if (group != null) 
    { 
        if (group.Groups != null) 
        { 
            int records = 0; 
            records = GetTotalRecordsCount(group); 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
        } 
        else if (group.Records != null) 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
    } 
 
    IRange summaryRange = null; 
    summaryRange = sheet.MigrantRange[excelRowIndex, excelExportingOptions.StartColumnIndex]; 
    ExportRowStyle(summaryRange, groupCaptionStyle, excelExportingOptions.RowIndex); 
    ExportSummaryCellToExcel(grid, summaryRange, ExportCellType.GroupCaptionCell, summaryDisplayTextForRow, null, string.Empty, excelExportingOptions); 
    if (excelExportingOptions.ExportStyle) 
        ExportGridStylesToExcel(grid.Style.CaptionSummaryRowStyle, summaryRange, excelExportingOptions); 
    excelRowIndex++; 
    this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, excelRowIndex); 
} 


Solution 2: 
You can change the back color and font of the caption summary in the exported excel sheet directly by using the following code.  

Code : 
protected override void ExportGroupCaptionToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Group group) 
{ 
 
    var excelRowIndex = (int)this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
    var excelColumnIndex = (int)this.GetType().BaseType.GetField("excelColumnIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
 
    //Below code merges All the GroupCaptionCells, if ShowSummaryInRow is True. 
    if ((grid.CaptionSummaryRow == null) || grid.CaptionSummaryRow.ShowSummaryInRow) 
    { 
        sheet.MigrantRange[excelRowIndex, 1, excelRowIndex, (1 + excelExportingOptions.Columns.Count) - 1].Merge(); 
    } 
 
    IStyle groupCaptionStyle; 
 
    if (sheet.Workbook.Styles.Contains("DataGridGroupCaptionCell")) 
        groupCaptionStyle = sheet.Workbook.Styles["DataGridGroupCaptionCell"]; 
    else 
        groupCaptionStyle = sheet.Workbook.Styles.Add("DataGridGroupCaptionCell"); 
 
    groupCaptionStyle.Color = Color.Blue; 
    groupCaptionStyle.Font.FontName = "Verdana"; 
    groupCaptionStyle.Font.Size = 10; 
 
    var summaryDisplayTextForRow = string.Empty; 
 
    if (group != null) 
    { 
        if (group.Groups != null) 
        { 
            int records = 0; 
            records = GetTotalRecordsCount(group); 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
        } 
        else if (group.Records != null) 
            summaryDisplayTextForRow = group.Key.ToString() + ": " + group.Records.Count + " Records"; 
    } 
 
    IRange summaryRange = null; 
    summaryRange = sheet.MigrantRange[excelRowIndex, excelExportingOptions.StartColumnIndex]; 
    ExportRowStyle(summaryRange, groupCaptionStyle, excelExportingOptions.RowIndex); 
    ExportSummaryCellToExcel(grid, summaryRange, ExportCellType.GroupCaptionCell, summaryDisplayTextForRow, null, string.Empty, excelExportingOptions); 
    excelRowIndex++; 
    this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, excelRowIndex); 
} 

 
Regards, 
Arulraj A 


Hi Arulraj A,

Thanks for your reply.

It working fine.

How to get the Column name also in the said example ?

Example : (Also check in attachment for reference)

ShipAddress =  Argentina: 412 Records

How to get Column name for all grouped heading ? 

Thanks
Deepak





Attachment: grouped_total_879d6870.rar


JP Jagadeesan Pichaimuthu Syncfusion Team March 18, 2019 12:25 PM UTC

Hi Deepak, 

Thanks for your update. 

Your requirement can be achieved by customizing the DrawCell event. Please find the code snippet and sample below, 

sfDataGrid.DrawCell += sfDataGrid_DrawCell; 

void sfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.CaptionCoveredRow && !string.IsNullOrEmpty(e.DisplayText)) 
    { 
        var displayText = ""; 
        string[] displayTextSplit = e.DisplayText.Split(':'); 
        var group = (e.DataRow.RowData as Syncfusion.Data.Group); 
        if (group != null) 
        { 
            if (group.Groups != null) 
            { 
                int records = 0; 
                records = GetTotalRecordsCount(group); 
                displayText += displayTextSplit[0] + "= " + group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
            } 
            else if (group.Records != null) 
                displayText = displayTextSplit[0] + "= " + group.Key.ToString() + ": " + group.Records.Count + " Records"; 
            e.DisplayText = displayText; 
        } 
    } 
} 


 

Let us know whether this helps also if you need any further assistance on this. 

Regards, 
Jagadeesan


DE Deepak March 18, 2019 01:59 PM UTC

Hi Jagadeesan,

Thanks for your reply.

The Said example is not working for export to excel

Thanks
Deepak


JP Jagadeesan Pichaimuthu Syncfusion Team March 19, 2019 12:47 PM UTC

Hi Deepak, 
 
Thanks for your update. 
 
You can able to achieve your requirement by customizing the ExportGroupCaptionToExcel method in DataGridToExcelConverterExt as like the below code snippet, 
 
public class DataGridToExcelConverterExt : DataGridToExcelConverter 
    { 
        protected override void ExportGroupCaptionToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Group group) 
        { 
            var excelRowIndex = (int)this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
            var excelColumnIndex = (int)this.GetType().BaseType.GetField("excelColumnIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this); 
 
            //Below code merges All the GroupCaptionCells, if ShowSummaryInRow is True. 
            if ((grid.CaptionSummaryRow == null) || grid.CaptionSummaryRow.ShowSummaryInRow) 
            { 
                sheet.MigrantRange[excelRowIndex, 1, excelRowIndex, (1 + excelExportingOptions.Columns.Count) - 1].Merge(); 
            } 
 
            IStyle groupCaptionStyle; 
 
            if (sheet.Workbook.Styles.Contains("DataGridGroupCaptionCell")) 
                groupCaptionStyle = sheet.Workbook.Styles["DataGridGroupCaptionCell"]; 
            else 
                groupCaptionStyle = sheet.Workbook.Styles.Add("DataGridGroupCaptionCell"); 
 
            var summaryDisplayTextForRow = string.Empty; 
            if (group != null) 
            { 
                string propertyName = (view.GroupDescriptions[group.Level - 1] as PropertyGroupDescription).PropertyName; 
                 var groupedColumn = grid.Columns.FirstOrDefault(col => col.MappingName == propertyName); 
                var columnHeaderText = (groupedColumn != null) ? groupedColumn.HeaderText : propertyName; 
 
                if (group.Groups != null) 
                { 
                    int records = 0; 
                    records = GetTotalRecordsCount(group); 
                    summaryDisplayTextForRow = columnHeaderText + " = " +  group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)"; 
                } 
                else if (group.Records != null) 
                    summaryDisplayTextForRow = columnHeaderText + " = " + group.Key.ToString() + ": " + group.Records.Count + " Records"; 
            } 
 
            IRange summaryRange = null; 
            summaryRange = sheet.MigrantRange[excelRowIndex, excelExportingOptions.StartColumnIndex]; 
            ExportRowStyle(summaryRange, groupCaptionStyle, excelExportingOptions.RowIndex); 
            ExportSummaryCellToExcel(grid, summaryRange, ExportCellType.GroupCaptionCell, summaryDisplayTextForRow, null, string.Empty, excelExportingOptions); 
            if (excelExportingOptions.ExportStyle) 
                ExportGridStylesToExcel(grid.Style.CaptionSummaryRowStyle, summaryRange, excelExportingOptions); 
            excelRowIndex++; 
            this.GetType().BaseType.GetField("excelRowIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, excelRowIndex); 
        } 
 
 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan

Loader.
Up arrow icon