/
Form1.cs
109 lines (101 loc) · 4.69 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
using DevExpress.DataAccess.Json;
using DevExpress.XtraReports.UI;
using System;
using System.Net;
using System.Windows.Forms;
namespace Create_a_Report_Bound_to_JsonDataSource
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Enables security protocol versions to access the JSON web data source.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
XtraReport report = CreateReport();
ReportDesignTool designTool = new ReportDesignTool(report);
designTool.ShowRibbonDesignerDialog();
Application.Exit();
}
private XtraReport CreateReport() {
XtraReport report = new XtraReport() {
Bands = {
new DetailBand() {
Controls = {
new XRLabel() {
ExpressionBindings = {
new ExpressionBinding("BeforePrint", "Text", "[CompanyName]")
},
WidthF = 300
}
},
HeightF = 50
}
},
DataSource = CreateDataSourceFromWeb(),
//DataSource = CreateDataSourceFromFile(),
//DataSource = CreateDataSourceFromText(),
DataMember = "Customers"
};
return report;
}
public static JsonDataSource CreateDataSourceFromWeb() {
var jsonDataSource = new JsonDataSource();
// Specify the endpoint.
jsonDataSource.JsonSource = new UriJsonSource(
new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
var root = new JsonSchemaNode();
root.NodeType = JsonNodeType.Object;
var customers = new JsonSchemaNode() {NodeType=JsonNodeType.Array, Name="Customers", Selected=true };
customers.AddChildren(new[] {
new JsonSchemaNode(new JsonNode("CustomerID", true,
JsonNodeType.Property, typeof(string)))
{
DisplayName = "Customer ID" },
new JsonSchemaNode() {
Name = "CompanyName",
Selected = true,
NodeType = JsonNodeType.Property,
Type = typeof(string)
},
new JsonSchemaNode(new JsonNode("ContactTitle", true, JsonNodeType.Property, typeof(string))),
new JsonSchemaNode(new JsonNode("Address", false, JsonNodeType.Property, typeof(string)))
});
root.AddChildren(customers);
jsonDataSource.Schema = root;
// The schema is built, you do not have to call the Fill method to populate the Field List.
// The Designer calls the Fill method automatically when a document is generated for preview.
//jsonDataSource.Fill();
return jsonDataSource;
}
public static JsonDataSource CreateDataSourceFromFile() {
var jsonDataSource = new JsonDataSource();
// Specify the JSON file name.
Uri fileUri = new Uri("customers.json", UriKind.RelativeOrAbsolute);
jsonDataSource.JsonSource = new UriJsonSource(fileUri);
// Populate the data source with data.
jsonDataSource.Fill();
return jsonDataSource;
}
public static JsonDataSource CreateDataSourceFromText() {
var jsonDataSource = new JsonDataSource();
// Specify a string with JSON data.
string json = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\"," +
"\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\"," +
"\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\"," +
"\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"}]," +
"\"ResponseStatus\":{}}";
// Specify the object that retrieves JSON data.
jsonDataSource.JsonSource = new CustomJsonSource(json);
// Populate the data source with data.
jsonDataSource.Fill();
return jsonDataSource;
}
}
}