Skip to content

Commit 1481306

Browse files
authored
Sample Added
Sample Added
1 parent 507df2a commit 1481306

13 files changed

+833
-0
lines changed

ExportedWithCheckBox.png

42.3 KB
Loading

ExporttheExcelfilewithCheckbox.gif

2.52 MB
Loading

Form1.Designer.cs

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Form1.cs

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
using Syncfusion.Data;
2+
using Syncfusion.WinForms.DataGrid;
3+
using System;
4+
using System.Collections.ObjectModel;
5+
using System.ComponentModel;
6+
using System.IO;
7+
using System.Windows.Forms;
8+
using Syncfusion.WinForms.DataGridConverter;
9+
using Syncfusion.XlsIO;
10+
11+
namespace SfDataGridDemo
12+
{
13+
/// <summary>
14+
/// Summary description for Form1.
15+
/// </summary>
16+
public partial class Form1 : Form
17+
{
18+
public Form1()
19+
{
20+
InitializeComponent();
21+
22+
sfDataGrid.AutoGenerateColumns = false;
23+
sfDataGrid.DataSource = new ViewModel().Orders;
24+
sfDataGrid.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
25+
sfDataGrid.AllowEditing = true;
26+
27+
GridNumericColumn gridTextColumn1 = new GridNumericColumn() { MappingName = "OrderID", HeaderText = "Order ID" };
28+
GridTextColumn gridTextColumn2 = new GridTextColumn() { MappingName = "CustomerID", HeaderText = "Customer ID" };
29+
GridTextColumn gridTextColumn3 = new GridTextColumn() { MappingName = "CustomerName", HeaderText = "Customer Name" };
30+
GridTextColumn gridTextColumn4 = new GridTextColumn() { MappingName = "Country", HeaderText = "Country" };
31+
GridTextColumn gridTextColumn5 = new GridTextColumn() { MappingName = "ShipCity", HeaderText = "Ship City" };
32+
GridCheckBoxColumn checkBoxColumn = new GridCheckBoxColumn() { MappingName = "IsShipped", HeaderText = "Is Shipped" };
33+
34+
sfDataGrid.Columns.Add(gridTextColumn1);
35+
sfDataGrid.Columns.Add(gridTextColumn2);
36+
sfDataGrid.Columns.Add(gridTextColumn3);
37+
sfDataGrid.Columns.Add(gridTextColumn4);
38+
sfDataGrid.Columns.Add(gridTextColumn5);
39+
sfDataGrid.Columns.Add(checkBoxColumn);
40+
41+
btnExportExcel.Click += BtnExportExcel_Click;
42+
}
43+
44+
ExcelExportingOptions GridExcelExportingOptions = new ExcelExportingOptions();
45+
46+
private ExcelExportingOptions ExcelExportingOptions1()
47+
{
48+
GridExcelExportingOptions.ExportAllPages = true;
49+
GridExcelExportingOptions.AllowOutlining = true;
50+
GridExcelExportingOptions.CellExporting += Options_CellExporting1;
51+
return GridExcelExportingOptions;
52+
}
53+
54+
private void BtnExportExcel_Click(object sender, EventArgs e)
55+
{
56+
var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, ExcelExportingOptions1());
57+
var workBook = excelEngine.Excel.Workbooks[0];
58+
59+
SaveFileDialog saveFilterDialog = new SaveFileDialog
60+
{
61+
FilterIndex = 2,
62+
Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx|Excel 2013 File(*.xlsx)|*.xlsx",
63+
FileName = "Sample1"
64+
};
65+
66+
if (saveFilterDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
67+
{
68+
using (Stream stream = saveFilterDialog.OpenFile())
69+
{
70+
if (saveFilterDialog.FilterIndex == 1)
71+
workBook.Version = ExcelVersion.Excel97to2003;
72+
else if (saveFilterDialog.FilterIndex == 2)
73+
workBook.Version = ExcelVersion.Excel2016;
74+
else
75+
workBook.Version = ExcelVersion.Excel2013;
76+
workBook.SaveAs(stream);
77+
}
78+
79+
//Message box confirmation to view the created workbook.
80+
if (MessageBox.Show(this.sfDataGrid, "Do you want to view the workbook?", "Workbook has been created",
81+
MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
82+
{
83+
84+
//Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
85+
System.Diagnostics.Process.Start(saveFilterDialog.FileName);
86+
}
87+
}
88+
}
89+
90+
private void Options_CellExporting1(object sender, Syncfusion.WinForms.DataGridConverter.Events.DataGridCellExcelExportingEventArgs e)
91+
{
92+
// Based on the column mapping name and the cell type, we can change the cell values while exporting to excel.
93+
if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsShipped")
94+
{
95+
//add the checkbox into excel shhet
96+
var checkbox = e.Range.Worksheet.CheckBoxes.AddCheckBox(e.Range.Row, e.Range.Column, 20, 20);
97+
98+
//set the checked or unchecked state based on cell value
99+
if (e.CellValue.ToString().ToLower() == "true")
100+
checkbox.CheckState = ExcelCheckState.Checked;
101+
else if (e.CellValue.ToString().ToLower() == "false")
102+
checkbox.CheckState = ExcelCheckState.Unchecked;
103+
104+
//Created check box with cell link
105+
checkbox.LinkedCell = e.Range.Worksheet[e.Range.AddressLocal];
106+
107+
e.Handled = true;
108+
}
109+
}
110+
}
111+
112+
public class OrderInfo : INotifyPropertyChanged
113+
{
114+
decimal? orderID;
115+
string customerId;
116+
string country;
117+
string customerName;
118+
string shippingCity;
119+
bool isShipped;
120+
121+
public OrderInfo()
122+
{
123+
124+
}
125+
126+
public decimal? OrderID
127+
{
128+
get { return orderID; }
129+
set { orderID = value; this.OnPropertyChanged("OrderID"); }
130+
}
131+
132+
public string CustomerID
133+
{
134+
get { return customerId; }
135+
set { customerId = value; this.OnPropertyChanged("CustomerID"); }
136+
}
137+
138+
public string CustomerName
139+
{
140+
get { return customerName; }
141+
set { customerName = value; this.OnPropertyChanged("CustomerName"); }
142+
}
143+
144+
public string Country
145+
{
146+
get { return country; }
147+
set { country = value; this.OnPropertyChanged("Country"); }
148+
}
149+
150+
public string ShipCity
151+
{
152+
get { return shippingCity; }
153+
set { shippingCity = value; this.OnPropertyChanged("ShipCity"); }
154+
}
155+
156+
public bool IsShipped
157+
{
158+
get { return isShipped; }
159+
set { isShipped = value; this.OnPropertyChanged("IsShipped"); }
160+
}
161+
162+
163+
public OrderInfo(decimal? orderId, string customerName, string country, string customerId, string shipCity, bool isShipped)
164+
{
165+
this.OrderID = orderId;
166+
this.CustomerName = customerName;
167+
this.Country = country;
168+
this.CustomerID = customerId;
169+
this.ShipCity = shipCity;
170+
this.IsShipped = isShipped;
171+
}
172+
173+
public event PropertyChangedEventHandler PropertyChanged;
174+
private void OnPropertyChanged(string propertyName)
175+
{
176+
if (PropertyChanged != null)
177+
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
178+
}
179+
}
180+
181+
public class ViewModel
182+
{
183+
private ObservableCollection<OrderInfo> orders;
184+
public ObservableCollection<OrderInfo> Orders
185+
{
186+
get { return orders; }
187+
set { orders = value; }
188+
}
189+
190+
public ViewModel()
191+
{
192+
orders = new ObservableCollection<OrderInfo>();
193+
orders.Add(new OrderInfo(1001, "Thomas Hardy", "Germany", "ALFKI", "Berlin", true));
194+
orders.Add(new OrderInfo(1002, "Laurence Lebihan", "Mexico", "ANATR", "Mexico", false));
195+
orders.Add(new OrderInfo(1003, "Antonio Moreno", "Mexico", "ANTON", "Mexico", true));
196+
orders.Add(new OrderInfo(1004, "Thomas Hardy", "UK", "AROUT", "London", true));
197+
orders.Add(new OrderInfo(1005, "Christina Berglund", "Sweden", "BERGS", "Lula", false));
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)