|
1 | 1 | # How-to-export-windows-forms-data-grid-stacked-headers-with-different-back-color-in-excel-and-pdf |
2 | 2 | How to export the Windows Forms DataGrid StackedHeaders with different back color in Excel and PDF? |
| 3 | + |
| 4 | +# How to export the WinForms DataGrid StackedHeaders with different back color to Excel and PDF? |
| 5 | + |
| 6 | +## About the sample |
| 7 | +This example illustrates how to export the WinForms DataGrid StackedHeaders with different back color to Excel and PDF |
| 8 | + |
| 9 | +By default, actual value only will be exported to Pdf. You can customize StackedHeader cellstyle by using PdfGridCellStyle instance through CellExporting event in PdfExportingOptions. |
| 10 | + |
| 11 | +```C# |
| 12 | + private void PDF_Exporting(object sender, EventArgs e) |
| 13 | + { |
| 14 | + PdfExportingOptions options = new PdfExportingOptions(); |
| 15 | + options.ExportStackedHeaders = true; |
| 16 | + options.CellExporting += options_CellExporting; |
| 17 | + var document = sfDataGrid1.ExportToPdf(options); |
| 18 | + document.Save("Sample.pdf"); |
| 19 | + } |
| 20 | + |
| 21 | + private void options_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) |
| 22 | + { |
| 23 | + if(e.CellType==ExportCellType.StackedHeaderCell) |
| 24 | + { |
| 25 | + if(e.CellValue.ToString()=="Order Details") |
| 26 | + { |
| 27 | + var cellStyle = new PdfGridCellStyle(); |
| 28 | + cellStyle.BackgroundBrush = PdfBrushes.DarkCyan; |
| 29 | + cellStyle.TextBrush = PdfBrushes.White; |
| 30 | + e.PdfGridCell.Style = cellStyle; |
| 31 | + } |
| 32 | + else if (e.CellValue.ToString() == "Customer Details") |
| 33 | + { |
| 34 | + var cellStyle = new PdfGridCellStyle(); |
| 35 | + cellStyle.BackgroundBrush = PdfBrushes.LightCyan; |
| 36 | + e.PdfGridCell.Style = cellStyle; |
| 37 | + } |
| 38 | + else if (e.CellValue.ToString() == "City Details") |
| 39 | + { |
| 40 | + var cellStyle = new PdfGridCellStyle(); |
| 41 | + cellStyle.BackgroundBrush = PdfBrushes.DarkGray; |
| 42 | + cellStyle.TextBrush = PdfBrushes.White; |
| 43 | + e.PdfGridCell.Style = cellStyle; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +Exporting SfDataGrid StackedHeaders with different back colors by using ExcelExportingOptions by defining the cell style for the StackedHeader cell range. |
| 52 | + |
| 53 | +```C# |
| 54 | + |
| 55 | +private void Excel_Exporting(object sender, EventArgs e) |
| 56 | +{ |
| 57 | + var options = new ExcelExportingOptions(); |
| 58 | + var excelEngine = sfDataGrid1.ExportToExcel(sfDataGrid1.View, options); |
| 59 | + var workBook = excelEngine.Excel.Workbooks[0]; |
| 60 | + workBook.Worksheets[0].Range["A1:A1"].CellStyle.Color = Color.DarkCyan; |
| 61 | + workBook.Worksheets[0].Range["A1:A1"].CellStyle.Font.Color = ExcelKnownColors.White; |
| 62 | + workBook.Worksheets[0].Range["B1:C1"].CellStyle.Color = Color.LightCyan; |
| 63 | + workBook.Worksheets[0].Range["B1:C1"].CellStyle.Font.Color = ExcelKnownColors.Black; |
| 64 | + workBook.Worksheets[0].Range["D1:E1"].CellStyle.Color = Color.DarkGray; |
| 65 | + workBook.Worksheets[0].Range["D1:E1"].CellStyle.Font.Color = ExcelKnownColors.White; |
| 66 | + workBook.SaveAs("SampleRange.xlsx"); |
| 67 | + } |
| 68 | +``` |
| 69 | + |
| 70 | +## Requirements to run the demo |
| 71 | +Visual Studio 2015 and above versions |
0 commit comments