(原)打印XtraGrid并设置表头、纸张类型
场景:
使用XtraGrid打印各种报表,要求能显示表头、条件,设置纸张类型等。
解决办法:
使用XtraGrid打印各种报表,要求能显示表头、条件,设置纸张类型等。
解决办法:
1
using System;
2
using System.Data;
3
using DevExpress.XtraPrintingLinks;
4
using DevExpress.XtraPrinting;
5
using System.Drawing;
6
using System.Drawing.Printing;
7![](/Images/OutliningIndicators/None.gif)
8![](/Images/OutliningIndicators/None.gif)
9
namespace Dhthx.Report
10
{
11
/// <summary>
12
/// ReportCenter 的摘要说明。
13
/// </summary>
14
public class ReportCenter
15
{
16
17
private PrintingSystem printSystem;
18
private string mReportName;
19
private string mCondition;
20![](/Images/OutliningIndicators/InBlock.gif)
21
public ReportCenter(IPrintable Printable)
22
{
23
printSystem = new PrintingSystem();
24
mReportName = "";
25
mCondition = "";
26
PrintableComponentLink pcl = new PrintableComponentLink();
27
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
28
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
29
pcl.Component = Printable;
30
printSystem.Links.Add(pcl);
31
pcl.CreateDocument();
32![](/Images/OutliningIndicators/InBlock.gif)
33
PrinterSettingsUsing pst = new PrinterSettingsUsing();
34
pst.UseMargins = false;
35
pst.UsePaperKind = false;
36
printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
37
printSystem.PageSettings.PaperName = "A4";
38
printSystem.PageSettings.LeftMargin = 2;
39
printSystem.PageSettings.RightMargin = 2;
40
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
41
}
42
public ReportCenter(IPrintable Printable, string ReportName)
43
{
44
printSystem = new PrintingSystem();
45
mReportName = ReportName;
46
mCondition = "";
47
PrintableComponentLink pcl = new PrintableComponentLink();
48
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
49
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
50
pcl.Component = Printable;
51
printSystem.Links.Add(pcl);
52
pcl.CreateDocument();
53![](/Images/OutliningIndicators/InBlock.gif)
54
PrinterSettingsUsing pst = new PrinterSettingsUsing();
55
pst.UseMargins = false;
56
pst.UsePaperKind = false;
57
printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
58
printSystem.PageSettings.PaperName = "A4";
59
printSystem.PageSettings.LeftMargin = 2;
60
printSystem.PageSettings.RightMargin = 2;
61
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
62
}
63
public ReportCenter(IPrintable Printable, string ReportName, string Condition)
64
{
65
printSystem = new PrintingSystem();
66
mReportName = ReportName;
67
mCondition = Condition;
68
PrintableComponentLink pcl = new PrintableComponentLink();
69
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
70
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
71
pcl.Component = Printable;
72
printSystem.Links.Add(pcl);
73
pcl.CreateDocument();
74![](/Images/OutliningIndicators/InBlock.gif)
75
PrinterSettingsUsing pst = new PrinterSettingsUsing();
76
pst.UseMargins = true;
77
//pst.UsePaperKind = false;
78
printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
79
printSystem.PageSettings.PaperName = "A4";
80
printSystem.PageSettings.LeftMargin = 30;
81
printSystem.PageSettings.RightMargin = 30;
82
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
83
}
84![](/Images/OutliningIndicators/InBlock.gif)
85
public ReportCenter(IPrintable Printable, PaperKind paperKind)
86
{
87
printSystem = new PrintingSystem();
88
mReportName = "";
89
mCondition = "";
90
PrintableComponentLink pcl = new PrintableComponentLink();
91
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
92
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
93
pcl.Component = Printable;
94
printSystem.Links.Add(pcl);
95
pcl.CreateDocument();
96![](/Images/OutliningIndicators/InBlock.gif)
97
PrinterSettingsUsing pst = new PrinterSettingsUsing();
98
pst.UseMargins = false;
99
pst.UsePaperKind = false;
100
printSystem.PageSettings.PaperKind = paperKind;
101
printSystem.PageSettings.PaperName = "A4";
102
printSystem.PageSettings.LeftMargin = 2;
103
printSystem.PageSettings.RightMargin = 2;
104
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
105
}
106
public ReportCenter(IPrintable Printable, string ReportName, PaperKind paperKind)
107
{
108
printSystem = new PrintingSystem();
109
mReportName = ReportName;
110
mCondition = "";
111
PrintableComponentLink pcl = new PrintableComponentLink();
112
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
113
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
114
pcl.Component = Printable;
115
printSystem.Links.Add(pcl);
116
pcl.CreateDocument();
117![](/Images/OutliningIndicators/InBlock.gif)
118
PrinterSettingsUsing pst = new PrinterSettingsUsing();
119
pst.UseMargins = true;
120
//pst.UsePaperKind = false;
121
printSystem.PageSettings.PaperKind = paperKind;
122
printSystem.PageSettings.PaperName = "A4";
123
printSystem.PageSettings.LeftMargin = 30;
124
printSystem.PageSettings.RightMargin = 30;
125
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
126
}
127
public ReportCenter(IPrintable Printable, string ReportName, string Condition,PaperKind paperKind)
128
{
129
printSystem = new PrintingSystem();
130
mReportName = ReportName;
131
mCondition = Condition;
132
PrintableComponentLink pcl = new PrintableComponentLink();
133
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
134
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
135
pcl.Component = Printable;
136
printSystem.Links.Add(pcl);
137
pcl.CreateDocument();
138![](/Images/OutliningIndicators/InBlock.gif)
139
PrinterSettingsUsing pst = new PrinterSettingsUsing();
140
pst.UseMargins = true;
141
//pst.UsePaperKind = false;
142
printSystem.PageSettings.PaperKind = paperKind;
143
//printSystem.PageSettings.PaperName = "A4";
144
printSystem.PageSettings.LeftMargin = 30;
145
printSystem.PageSettings.RightMargin = 30;
146
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
147
}
148![](/Images/OutliningIndicators/InBlock.gif)
149
private void link_CreateMarginalHeaderArea(object sender, DevExpress.XtraPrinting.CreateAreaEventArgs e)
150
{
151
if (mReportName != "")
152
{
153
e.Graph.Font = new Font("宋体", 15, FontStyle.Bold);
154
e.Graph.BackColor = Color.Transparent;
155
RectangleF r = new RectangleF(0, 20, 0, e.Graph.Font.Height + 20);
156
PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mReportName, Color.Black, r, BorderSide.None);
157
brick.Alignment = BrickAlignment.Center;
158
brick.AutoWidth = true;
159
}
160![](/Images/OutliningIndicators/InBlock.gif)
161
if (mCondition != "")
162
{
163
e.Graph.Font = new Font("宋体", 10);
164
e.Graph.BackColor = Color.Transparent;
165
RectangleF r = new RectangleF(0, 50, 0, e.Graph.Font.Height + 20);
166
PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mCondition, Color.Black, r, BorderSide.None);
167
brick.Alignment = BrickAlignment.Center;
168
brick.AutoWidth = true;
169
}
170
}
171
private void link_CreateMarginalFooterArea(object sender, CreateAreaEventArgs e)
172
{
173
string format = "第{0}页 共{1}页";
174
e.Graph.Font = new Font("宋体", 10);
175
e.Graph.BackColor = Color.Transparent;
176![](/Images/OutliningIndicators/InBlock.gif)
177
RectangleF r = new RectangleF(0, 5, 0, e.Graph.Font.Height + 20);
178![](/Images/OutliningIndicators/InBlock.gif)
179
PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, format, Color.Black, r, BorderSide.None);
180
brick.Alignment = BrickAlignment.Far;
181
brick.AutoWidth = true;
182![](/Images/OutliningIndicators/InBlock.gif)
183
brick = e.Graph.DrawPageInfo(PageInfo.DateTime, "打印时间:" + DateTime.Today.ToLongDateString(), Color.Black, r, BorderSide.None);
184
brick.Alignment = BrickAlignment.Near;
185
brick.AutoWidth = true;
186
}public void Print()
187
{
188
printSystem.Print();
189
}
190
191
public void Preview()
192
{
193
FrmPreview frm = new FrmPreview(printSystem);
194
frm.Show();
195
}
196
public void Designe()
197
{}
198![](/Images/OutliningIndicators/InBlock.gif)
199
200
}
201
}
202![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/InBlock.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/InBlock.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/InBlock.gif)
139
![](/Images/OutliningIndicators/InBlock.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/InBlock.gif)
142
![](/Images/OutliningIndicators/InBlock.gif)
143
![](/Images/OutliningIndicators/InBlock.gif)
144
![](/Images/OutliningIndicators/InBlock.gif)
145
![](/Images/OutliningIndicators/InBlock.gif)
146
![](/Images/OutliningIndicators/InBlock.gif)
147
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
148
![](/Images/OutliningIndicators/InBlock.gif)
149
![](/Images/OutliningIndicators/InBlock.gif)
150
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
151
![](/Images/OutliningIndicators/InBlock.gif)
152
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
153
![](/Images/OutliningIndicators/InBlock.gif)
154
![](/Images/OutliningIndicators/InBlock.gif)
155
![](/Images/OutliningIndicators/InBlock.gif)
156
![](/Images/OutliningIndicators/InBlock.gif)
157
![](/Images/OutliningIndicators/InBlock.gif)
158
![](/Images/OutliningIndicators/InBlock.gif)
159
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
160
![](/Images/OutliningIndicators/InBlock.gif)
161
![](/Images/OutliningIndicators/InBlock.gif)
162
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
163
![](/Images/OutliningIndicators/InBlock.gif)
164
![](/Images/OutliningIndicators/InBlock.gif)
165
![](/Images/OutliningIndicators/InBlock.gif)
166
![](/Images/OutliningIndicators/InBlock.gif)
167
![](/Images/OutliningIndicators/InBlock.gif)
168
![](/Images/OutliningIndicators/InBlock.gif)
169
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
170
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
171
![](/Images/OutliningIndicators/InBlock.gif)
172
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
173
![](/Images/OutliningIndicators/InBlock.gif)
174
![](/Images/OutliningIndicators/InBlock.gif)
175
![](/Images/OutliningIndicators/InBlock.gif)
176
![](/Images/OutliningIndicators/InBlock.gif)
177
![](/Images/OutliningIndicators/InBlock.gif)
178
![](/Images/OutliningIndicators/InBlock.gif)
179
![](/Images/OutliningIndicators/InBlock.gif)
180
![](/Images/OutliningIndicators/InBlock.gif)
181
![](/Images/OutliningIndicators/InBlock.gif)
182
![](/Images/OutliningIndicators/InBlock.gif)
183
![](/Images/OutliningIndicators/InBlock.gif)
184
![](/Images/OutliningIndicators/InBlock.gif)
185
![](/Images/OutliningIndicators/InBlock.gif)
186
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
187
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
188
![](/Images/OutliningIndicators/InBlock.gif)
189
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
190
![](/Images/OutliningIndicators/InBlock.gif)
191
![](/Images/OutliningIndicators/InBlock.gif)
192
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
193
![](/Images/OutliningIndicators/InBlock.gif)
194
![](/Images/OutliningIndicators/InBlock.gif)
195
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
196
![](/Images/OutliningIndicators/InBlock.gif)
197
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
198
![](/Images/OutliningIndicators/InBlock.gif)
199
![](/Images/OutliningIndicators/InBlock.gif)
200
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
201
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
202
![](/Images/OutliningIndicators/None.gif)
1
//使用方法
2
ReportCenter rc = new ReportCenter(gControl, lblTitle.Text, strDate);
3
rc.Preview();
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)