| 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" } }, }; |
| 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 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;
}
}
} |
| 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; } |
| 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; } } |
| var dataGridToPdfConverter = new DataGridToPdfConverterExt(); var pdfGrid = dataGridToPdfConverter.ExportToPdfGrid(sfDataGrid, sfDataGrid.View, options); pdfGrid.Draw(page, new PointF()); |
|
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);
} |
|
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);
} |
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"];elsegroupCaptionStyle = 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);}
Sample Location : http://www.syncfusion.com/downloads/support/directtrac/general/ze/CS_Excel_ExportStyle1044878045
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"];elsegroupCaptionStyle = 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);}
Sample Location : http://www.syncfusion.com/downloads/support/directtrac/general/ze/CS__Excel_ChangeColor-1431253231Regards,Arulraj A
| 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; } } } |
| 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); } |