-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
110 lines (102 loc) · 5.04 KB
/
Form1.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
using System;
using System.Windows.Forms;
#region usings_sql
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
#endregion
#region using_datafederation
using DevExpress.DataAccess.DataFederation;
#endregion
#region using_excel
using DevExpress.DataAccess.Excel;
using System.IO;
#endregion
#region usings_report
using System.ComponentModel;
using System.Drawing;
using DevExpress.XtraReports.UI;
#endregion
namespace BindReportToFederatedUnionQuery {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
void Button1_Click(object sender, EventArgs e) {
#region ShowDesigner
ReportDesignTool designTool = new ReportDesignTool(CreateReport());
designTool.ShowRibbonDesignerDialog();
#endregion
}
#region CreateFederationDataSource
static FederationDataSource CreateFederationDataSource(SqlDataSource sql, ExcelDataSource excel) {
// Create a federated query's SQL and Excel sources.
Source sqlSource = new Source(sql.Name, sql, "Customers");
Source excelSource = new Source(excel.Name, excel, "");
// Create a federated Union query.
var contactsNode = sqlSource.From()
// Select the "ContactName", "City" and "Phone" columns from the SQL source.
.Select("ContactName", "City", "Phone")
.Build()
// Union the SQL source with the Excel source.
.Union(excelSource.From()
// Select the "ContactName", "City" and "Phone" columns from the Excel source.
.Select("ContactName", "City", "Phone").Build(),
UnionType.Union)
// Specify the query's name and build it.
.Build("Contacts");
// Create a federated data source and add the federated query to the collection.
var federationDataSource = new FederationDataSource();
federationDataSource.Queries.Add(contactsNode);
// Build the data source schema to display it in the Field List.
federationDataSource.RebuildResultSchema();
return federationDataSource;
}
#endregion
#region CreateReport
public static XtraReport CreateReport() {
// Create a new report.
var report = new XtraReport();
// Create data sources.
var sqlDataSource = CreateSqlDataSource();
var excelDataSource = CreateExcelDataSource();
var federationDataSource = CreateFederationDataSource(sqlDataSource, excelDataSource);
// Add all data sources to the report to avoid serialization issues.
report.ComponentStorage.AddRange(new IComponent[] { sqlDataSource, excelDataSource, federationDataSource });
// Assign a federated data source to the report.
report.DataSource = federationDataSource;
report.DataMember = "Contacts";
// Add the Detail band and labels bound to the federated data source's fields.
var detailBand = new DetailBand() { HeightF = 50 };
report.Bands.Add(detailBand);
var contactNameLabel = new XRLabel() { WidthF = 150 };
var cityLabel = new XRLabel() { WidthF = 150, LocationF = new PointF(200, 0) };
var phoneLabel = new XRLabel() { WidthF = 200, LocationF = new PointF(400, 0) };
contactNameLabel.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[ContactName]"));
cityLabel.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[City]"));
phoneLabel.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[Phone]"));
detailBand.Controls.AddRange(new[] { contactNameLabel, cityLabel, phoneLabel });
return report;
}
#endregion
#region CreateSqlDataSource
static SqlDataSource CreateSqlDataSource() {
var connectionParameters = new SQLiteConnectionParameters("Data/nwind.db", null); var sqlDataSource = new SqlDataSource(connectionParameters) { Name = "Sql_Customers" };
var categoriesQuery = SelectQueryFluentBuilder.AddTable("Customers").SelectAllColumnsFromTable().Build("Customers");
sqlDataSource.Queries.Add(categoriesQuery);
sqlDataSource.RebuildResultSchema();
return sqlDataSource;
}
#endregion
#region CreateExcelDataSource
static ExcelDataSource CreateExcelDataSource() {
var excelDataSource = new ExcelDataSource() { Name = "Excel_Suppliers" };
excelDataSource.FileName = Path.Combine(Path.GetDirectoryName(typeof(Form1).Assembly.Location), "Data/Suppliers.xlsx");
excelDataSource.SourceOptions = new ExcelSourceOptions() {
ImportSettings = new ExcelWorksheetSettings("Sheet"),
};
excelDataSource.RebuildResultSchema();
return excelDataSource;
}
#endregion
}
}