-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
117 lines (107 loc) · 5.77 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using DevExpress.Spreadsheet;
using System.ComponentModel;
namespace DataBindingToListExample {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : DevExpress.Xpf.Ribbon.DXRibbonWindow {
WorksheetDataBinding weatherDataBinding;
WorksheetDataBinding fishesDataBinding;
public MainWindow() {
InitializeComponent();
ribbonControl1.SelectedPage = barPageExample;
#region #ErrorSubscribe
spreadsheetControl1.Document.Worksheets[0].DataBindings.Error += DataBindings_Error;
#endregion #ErrorSubscribe
}
#region #ErrorHandler
private void DataBindings_Error(object sender, DataBindingErrorEventArgs e) {
MessageBox.Show(String.Format("Error at worksheet.Rows[{0}].\n The error is : {1}", e.RowIndex, e.ErrorType.ToString()), "Binding Error");
}
#endregion #ErrorHandler
private void barBtnBindWeather_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
BindWeatherReport(MyWeatherReportSource.Data);
}
private void barBtnWeatherBindingList_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
BindWeatherReport(MyWeatherReportSource.DataAsBindingList);
}
private void BindWeatherReport(object weatherDatasource) {
if (this.weatherDataBinding != null)
spreadsheetControl1.Document.Worksheets[0].DataBindings.Remove(this.weatherDataBinding);
#region #BindTheList
// Specify the binding options.
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
dsOptions.ImportHeaders = true;
dsOptions.CellValueConverter = new MyWeatherConverter();
dsOptions.SkipHiddenRows = true;
// Bind the data source to the worksheet range.
Worksheet sheet = spreadsheetControl1.Document.Worksheets[0];
WorksheetDataBinding sheetDataBinding = sheet.DataBindings.BindToDataSource(weatherDatasource, 2, 1, dsOptions);
#endregion #BindTheList
this.weatherDataBinding = sheetDataBinding;
// Highlight the binding range.
this.weatherDataBinding.Range.FillColor = System.Drawing.Color.Lavender;
// Adjust column width.
spreadsheetControl1.Document.Worksheets[0].Range.FromLTRB(1, 1, this.weatherDataBinding.Range.RightColumnIndex, this.weatherDataBinding.Range.BottomRowIndex).AutoFitColumns();
}
private void barBtnAddWeatherReport_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
MyWeatherReportSource.DataAsBindingList.Insert(1, new WeatherReport() {
Date = new DateTime(1776, 2, 29),
Weather = Weather.Sunny,
HourlyReport = MyWeatherReportSource.GenerateRandomHourlyReport()
});
}
#region My Fishes
private void barBtnBindMyFishes_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
// Specify the binding options.
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
dsOptions.ImportHeaders = true;
// Bind the data source to the worksheet range.
this.fishesDataBinding = spreadsheetControl1.Document.Worksheets[0].DataBindings.BindToDataSource(MyFishesSource.Data, 2, 5, dsOptions);
// Highlight the binding range.
this.fishesDataBinding.Range.FillColor = System.Drawing.Color.LightCyan;
// Adjust column width.
spreadsheetControl1.Document.Worksheets[0].Range.FromLTRB(5, 2, this.fishesDataBinding.Range.RightColumnIndex, this.fishesDataBinding.Range.BottomRowIndex).AutoFitColumns();
}
#endregion My Fishes
private void barBtnImport_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
if (this.fishesDataBinding != null) {
this.fishesDataBinding.Range.FillColor = System.Drawing.Color.Empty;
spreadsheetControl1.Document.Worksheets[0].DataBindings.Remove(this.fishesDataBinding);
}
int columnCount = DisplayColumnHeaders(MyFishesSource.Data, 2, 5);
spreadsheetControl1.Document.Worksheets[0].Import(MyFishesSource.Data, 3, 5);
spreadsheetControl1.Document.Worksheets[0].Range.FromLTRB(5, 2, 5 + columnCount, 2 + MyFishesSource.Data.Count).AutoFitColumns();
}
private void barBtnUnbind_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
foreach (WorksheetDataBinding wdb in spreadsheetControl1.Document.Worksheets[0].DataBindings)
wdb.Range.FillColor = System.Drawing.Color.Empty;
weatherDataBinding = null;
fishesDataBinding = null;
spreadsheetControl1.Document.Worksheets[0].DataBindings.Clear();
}
private int DisplayColumnHeaders(object dataSource, int topRow, int leftColumn) {
// Get column headers from the data source
PropertyDescriptorCollection pdc = DataSourceHelper.GetSourceProperties(dataSource);
for (int i = 0; i < pdc.Count; i++) {
PropertyDescriptor pd = pdc[i];
spreadsheetControl1.ActiveWorksheet[topRow, i + leftColumn].Value = pd.DisplayName;
}
return pdc.Count;
}
}
}