diff --git a/ExportingToExcel.png b/ExportingToExcel.png new file mode 100644 index 0000000..c4250d0 Binary files /dev/null and b/ExportingToExcel.png differ diff --git a/ExportingToPDF.png b/ExportingToPDF.png new file mode 100644 index 0000000..105c703 Binary files /dev/null and b/ExportingToPDF.png differ diff --git a/README.md b/README.md index 73d39ff..8f04110 100644 --- a/README.md +++ b/README.md @@ -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(); @@ -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) \ No newline at end of file