1, 此报表在设置时,paperkind中找不到自定义的纸张,所以我就在事件beforeprint中,
this.Margins = newSystem.Drawing.Printing.Margins(10, 10, 10, 10);
this.PaperKind =System.Drawing.Printing.PaperKind.Custom;
this.PageWidth = 827;
this.PageHeight = 584;
this.PaperName = "21*13"
2.加载已有模板
xrpt1.LoadLayout(@"E:\EMS.repx");
3.实现主从表的绑定。
1 | DataColumn parentColumns; |
2 | DataColumn childColumns; | |
3 | parentColumns = ds.Tables["Suppliers"].Columns["SupplierID"]; |
4 | childColumns = ds.Tables["Products"].Columns["SupplierID"]; | |
5 | DataRelation dsdr1 = new DataRelation("fk_1", parentColumns, childColumns); |
6 | ds.Relations.Add(dsdr1); | |
7 |
|
8 | DataRelation dsdr2 = new DataRelation("fk_2", ds.Tables["Products"].Columns["ProductID"], ds.Tables["OrderDetails"].Columns["ProductID"]); | |
9 | ds.Relations.Add(dsdr2); |
而模板中绑定数据的话跟上面单表是的大同小异,但是里面的绑定要记得引用的是你绑定关系的外键,也就是,如下代码:
01 | public XtraReport1(DataSet ds) | |
02 | { |
03 | InitializeComponent(); | |
04 | SetDataBing(ds); |
05 | } | |
06 | private void SetDataBing(DataSet ds) |
07 | { | |
09 | DataMember = "Suppliers"; | |
10 | DataSource = ds; |
13 | this.cellCompanyName.DataBindings.Add("Text", DataSource, "Suppliers.CompanyName"); |
14 | this.sContadName.DataBindings.Add("Text", DataSource, "Suppliers.ContadName"); |
15 | this.sCountry.DataBindings.Add("Text", DataSource, "Suppliers.Country"); |
16 | this.sContactTitle.DataBindings.Add("Text", DataSource, "Suppliers.ContactTitle"); |
17 | this.sRegion.DataBindings.Add("Text", DataSource, "Suppliers.Region"); |
18 | this.sPhone.DataBindings.Add("Text", DataSource, "Suppliers.Phone"); |
19 | this.sCity.DataBindings.Add("Text", DataSource, "Suppliers.City"); |
20 | this.sFax.DataBindings.Add("Text", DataSource, "Suppliers.Fax"); |
21 | this.sPostalCode.DataBindings.Add("Text", DataSource, "Suppliers.PostalCode"); |
22 | this.sHomePage.DataBindings.Add("Text", DataSource, "Suppliers.HomePage"); |
23 | this.sAddress.DataBindings.Add("Text", DataSource, "Suppliers.Address"); |
24 | this.sCompanyName.DataBindings.Add("Text", DataSource, "Suppliers.CompanyName"); |
25 |
| |
26 | DetailReport.DataMember = "fk_1"; |
27 | DetailReport.DataSource = DataSource; |
29 | this.pProductName.DataBindings.Add("Text", DataSource, "fk_1.ProductName"); |
30 | this.pProductID.DataBindings.Add("Text", DataSource, "fk_1.ProductID"); |
31 | this.pCategory.DataBindings.Add("Text", DataSource, "fk_1.CategoryID"); |
32 | this.pUnit.DataBindings.Add("Text", DataSource, "fk_1.QuantityPerUnit"); |
33 | this.pUnitPrice.DataBindings.Add("Text", DataSource, "fk_1.UnitPrice"); |
34 | this.pDiscontinued.DataBindings.Add("Text", DataSource, "fk_1.Discontinued"); |
36 | DetailReport1.DataMember = "fk_1.fk_2"; |
37 | DetailReport1.DataSource = DataSource; |
39 | this.oOrderID.DataBindings.Add("Text", DataSource, "fk_1.fk_2.OrderID"); |
40 | this.oQuantity.DataBindings.Add("Text", DataSource, "fk_1.fk_2.Quantity"); |
41 | this.oDiscount.DataBindings.Add("Text", DataSource, "fk_1.fk_2.Discount"); |
43 | this.oUnitPrice.DataBindings.Add("Text", DataSource, "fk_1.fk_2.UnitPrice"); |
46 | } | ||
| this.pProductName.DataBindings.Add("Text", DataSource, "fk_1.ProductName")以这句来说明,"fk_1.ProductName" |
| 这个就是子表的数据字段,这部是主从表动态绑定的关键,这样数据就会随主表数据变化而变化。 |
14 | namespace WFAXtraReport |
15 | { | |
16 | public partial class Form1 : Form |
17 | { | |
18 | XtraReport r ;//这个可以是加载之前设计好的模板 |
19 | public Form1() | |
20 | { |
21 | InitializeComponent(); | |
22 | } |
23 |
| |
24 | private void designForm_FormClosing(object sender, FormClosingEventArgs e) |
25 | { | |
26 | //在此处处理关闭设计器时的操作,主要用来自定义保存数据 |
27 | //r.SaveLayout(@"C:\1.repx"); | |
28 | } |
|
| ||
30 | private void designForm_ReportStateChanged(object sender, ReportStateEventArgs e) |
31 | { | |
32 | //只要报表发生改变就立即将状态设置为保存 |
33 | //避免系统默认保存对话框的出现 | |
34 | if (e.ReportState == ReportState.Changed) |
35 | { | ||
36 | ((XRDesignFormEx)sender).DesignPanel.ReportState = ReportState.Saved; |
37 | } | |
38 | } |
39 |
| |
40 | private void Form1_Load(object sender, EventArgs e) |
41 | { | |
42 | r = new XtraReport(); |
43 | //r.LoadLayout(@"C:\1.repx"); | |
44 | XRDesignFormEx designForm = new XRDesignFormEx(); |
45 |
| |
46 | //隐藏按钮 |
47 | designForm.DesignPanel.SetCommandVisibility(new ReportCommand[]{ | ||
48 | ReportCommand.NewReport, |
49 | ReportCommand.SaveFileAs, | |
50 | ReportCommand.NewReportWizard, |
51 | ReportCommand.OpenFile | |
52 | }, CommandVisibility.None); |
53 |
|
54 |
|
55 | //更改状态 | |
56 | designForm.ReportStateChanged += new ReportStateEventHandler(designForm_ReportStateChanged); |
58 | designForm.FormClosing += new FormClosingEventHandler(designForm_FormClosing); |
60 | // 加载报表. |
61 | designForm.OpenReport(r); | |
62 |
|
63 | // 打开设计器 | |
64 | designForm.ShowDialog(); |
65 |
| |
66 | designForm.Dispose(); |
67 | } | |
68 | } |
69 | } |