Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ExportingToExcel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ExportingToPDF.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 41 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
# How to export the WinForms DataGrid StackedHeaders with different back color to Excel and PDF?
# How to Export the WinForms DataGrid StackedHeaders with Different BackColor to Excel and PDF?

## About the sample
This example illustrates how to export the WinForms DataGrid StackedHeaders with different back color to Excel and PDF
This example illustrates how to export the [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid) StackedHeaders with different back color to Excel and PDF

By default, actual value only will be exported to Pdf. You can customize StackedHeader cellstyle by using PdfGridCellStyle instance through CellExporting event in PdfExportingOptions.
## Pdf Exporting

By default, background color is not applied for [StackedHeaderRow](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.StackedHeaderRow.html) when export a `DataGrid` to PDF, you can apply the back color by using [PdfGridCell](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Pdf.Grid.PdfGridCell.html) in [PdfExportingOptions.CellExporting](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGridConverter.ExcelExportingOptions.html#Syncfusion_WinForms_DataGridConverter_ExcelExportingOptions_CellExporting) event.

```C#
private void PDF_Exporting(object sender, EventArgs e)
{
PdfExportingOptions options = new PdfExportingOptions();
options.ExportStackedHeaders = true;
options.CellExporting += options_CellExporting;
var document = sfDataGrid1.ExportToPdf(options);
document.Save("Sample.pdf");
}
private void PDF_Exporting(object sender, EventArgs e)
{
PdfExportingOptions options = new PdfExportingOptions();
options.ExportStackedHeaders = true;
options.CellExporting += options_CellExporting;
var document = sfDataGrid1.ExportToPdf(options);
document.Save("Sample.pdf");
}

private void options_CellExporting(object sender, DataGridCellPdfExportingEventArgs e)
private void options_CellExporting(object sender, DataGridCellPdfExportingEventArgs e)
{
if(e.CellType==ExportCellType.StackedHeaderCell)
{
if(e.CellType==ExportCellType.StackedHeaderCell)
if(e.CellValue.ToString()=="Order Details")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.DarkCyan;
cellStyle.TextBrush = PdfBrushes.White;
e.PdfGridCell.Style = cellStyle;
}
else if (e.CellValue.ToString() == "Customer Details")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.LightCyan;
e.PdfGridCell.Style = cellStyle;
}
else if (e.CellValue.ToString() == "City Details")
{
if(e.CellValue.ToString()=="Order Details")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.DarkCyan;
cellStyle.TextBrush = PdfBrushes.White;
e.PdfGridCell.Style = cellStyle;
}
else if (e.CellValue.ToString() == "Customer Details")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.LightCyan;
e.PdfGridCell.Style = cellStyle;
}
else if (e.CellValue.ToString() == "City Details")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.DarkGray;
cellStyle.TextBrush = PdfBrushes.White;
e.PdfGridCell.Style = cellStyle;
}
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.DarkGray;
cellStyle.TextBrush = PdfBrushes.White;
e.PdfGridCell.Style = cellStyle;
}
}
}
}
```

![Exporting the StackedHeaders to PDF](ExportingToPDF.png)

Exporting SfDataGrid StackedHeaders with different back colors by using ExcelExportingOptions by defining the cell style for the StackedHeader cell range.
## Excel Exporting

```C#
By default, background color is not applied for [StackedHeaderRow](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.StackedHeaderRow.html) when export a SfDataGrid to excel, you can apply the back color by set the color for specific cell range in exported work book.

```C#
private void Excel_Exporting(object sender, EventArgs e)
{
var options = new ExcelExportingOptions();
Expand All @@ -64,5 +66,4 @@ private void Excel_Exporting(object sender, EventArgs e)
}
```

## Requirements to run the demo
Visual Studio 2015 and above versions
![Exporting the StackedHeaders to Excel](ExportingToExcel.png)