/
Form1.cs
130 lines (116 loc) · 4.83 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Wizard.Services;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;
using System.ComponentModel.Design;
using System;
namespace RuntimeSqlDataSourceReportSample
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
SqlDataSource DataSource { get; set; }
public Form1()
{
InitializeComponent();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
OpenReportDesigner();
}
private void OpenReportDesigner()
{
CreateSqlDataSource();
XtraReport rep = ReportCreator.CreateReport(DataSource);
ReportDesignTool designer = new ReportDesignTool(rep);
designer.ShowRibbonDesignerDialog();
}
public void CreateSqlDataSource()
{
InitializeSqlDataSource();
AddQueryRelations();
DataSource.Queries.Add(QueryHelper.CreateSelectQuery());
DataSource.Queries.Add(QueryHelper.CreateCustomSqlQuery());
DataSource.Queries.Add(QueryHelper.CreateStoredProcedureQuery());
DataSource.RebuildResultSchema();
}
void InitializeSqlDataSource()
{
//CreateConnectionFromParameters();
CreateConnectionFromAppConfig();
//CreateConnectionFromString();
//UseConfigureDataConnectionEvent();
}
private void CreateConnectionFromParameters()
{
MsSqlConnectionParameters connectionParameters = new MsSqlConnectionParameters()
{
ServerName = "localhost",
DatabaseName = "NorthWind",
UserName = null,
Password = null,
AuthorizationType = MsSqlAuthorizationType.Windows
};
DataSource = new SqlDataSource(connectionParameters);
}
private void CreateConnectionFromAppConfig()
{
DataSource = new SqlDataSource("TestConnectionString");
}
private void CreateConnectionFromString()
{
string connectionString = "XpoProvider=MSSqlServer;Data Source=(LocalDB)\\MSSQLLocalDB;" +
"AttachDbFilename=|DataDirectory|\\Test.mdf;" +
"Integrated Security=True;Connect Timeout=30";
CustomStringConnectionParameters connectionParameters =
new CustomStringConnectionParameters(connectionString);
DataSource = new SqlDataSource(connectionParameters);
}
void AddQueryRelations()
{
SelectQuery categories = SelectQueryFluentBuilder
.AddTable("Categories")
.SelectAllColumns()
.Build("Categories");
SelectQuery products = SelectQueryFluentBuilder
.AddTable("Products")
.SelectAllColumns()
.Build("Products");
DataSource.Queries.AddRange(new SqlQuery[] { categories, products });
DataSource.Relations.Add(
new MasterDetailInfo("Categories", "Products", "CategoryID", "CategoryID"));
}
private void simpleButton2_Click(object sender, EventArgs e)
{
OpenReportDesignerWithService();
}
private void OpenReportDesignerWithService()
{
CustomConnectionProviderService connectionProviderService =
new CustomConnectionProviderService();
DataSource = new SqlDataSource("MyRuntimeConnection");
AddQueryRelations();
XtraReport rep = ReportCreator.CreateReport(DataSource);
ReportDesignTool designer = new ReportDesignTool(rep);
ReplaceService(designer.DesignRibbonForm.DesignMdiController,
typeof(IConnectionProviderService),
connectionProviderService);
designer.DesignRibbonForm.DesignMdiController.DesignPanelLoaded +=
DesignMdiControllerOnDesignPanelLoaded;
designer.ShowRibbonDesignerDialog();
}
private void ReplaceService(IServiceContainer container,
Type serviceType,
object serviceInstance)
{
if (container.GetService(serviceType) != null)
container.RemoveService(serviceType);
container.AddService(serviceType, serviceInstance);
}
private void DesignMdiControllerOnDesignPanelLoaded(object sender, DesignerLoadedEventArgs e)
{
ReplaceService(e.DesignerHost, typeof(IConnectionProviderService),
new CustomConnectionProviderService());
}
}
}