|
1 | | -# how-to-export-the-excel-file-with-checkbox-in-winforms-datagrid |
2 | | -How to Export the excel file with checkbox in WinForms DataGrid (SfDataGid)? |
| 1 | +# How to Export the excel file with checkbox in WinForms DataGrid (SfDataGid)? |
| 2 | + |
| 3 | +## About the sample |
| 4 | +This example illustrates how to Export the excel file with checkbox in [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid)? |
| 5 | + |
| 6 | +[WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid) does not provide the direct support to export the excel file with a checkbox. You can export the excel file with a checkbox by customization the [CellExporting](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGridConverter.ExcelExportingOptions.html#Syncfusion_WinForms_DataGridConverter_ExcelExportingOptions_CellExporting) event of the [ExcelExportingOptions](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGridConverter.ExcelExportingOptions.html) in [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid). |
| 7 | + |
| 8 | +```C# |
| 9 | + |
| 10 | +GridExcelExportingOptions.CellExporting += Options_CellExporting1; |
| 11 | + |
| 12 | +private void Options_CellExporting1(object sender, Syncfusion.WinForms.DataGridConverter.Events.DataGridCellExcelExportingEventArgs e) |
| 13 | +{ |
| 14 | + // Based on the column mapping name and the cell type, we can change the cell values while exporting to excel. |
| 15 | + if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsShipped") |
| 16 | + { |
| 17 | + //add the checkbox into excel shhet |
| 18 | + var checkbox = e.Range.Worksheet.CheckBoxes.AddCheckBox(e.Range.Row, e.Range.Column, 20, 20); |
| 19 | + |
| 20 | + //set the checked or unchecked state based on cell value |
| 21 | + if (e.CellValue.ToString().ToLower() == "true") |
| 22 | + checkbox.CheckState = ExcelCheckState.Checked; |
| 23 | + else if (e.CellValue.ToString().ToLower() == "false") |
| 24 | + checkbox.CheckState = ExcelCheckState.Unchecked; |
| 25 | + |
| 26 | + //Created check box with cell link |
| 27 | + checkbox.LinkedCell = e.Range.Worksheet[e.Range.AddressLocal]; |
| 28 | + |
| 29 | + e.Handled = true; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +The following screenshot shows the exported excel file with a checkbox, |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +Take a moment to peruse the [WinForms DataGrid – Export to Excel](https://help.syncfusion.com/windowsforms/datagrid/exporttoexcel) documentation, where you can find about export to excel with code examples. |
| 42 | + |
| 43 | +## Requirements to run the demo |
| 44 | +Visual Studio 2015 and above versions |
0 commit comments