XtraReport报表编辑器里的保存,是可以保存为一个文件的,所以它应该提供了一个从文件加载的方法,这时我们可以发现XtraReport里有一个LoadLayout的方法,可以加载报表文件,它的重载方法是可以从IO.Stream里加载报表文件,也就是说,我们可以进一步的把这个报表模板以二进制的方式保存在数据库里。需要的时候,从数据库调用即可。
我们在设计XtraReport的模板的时候,重写它的析构方法,一个有传入数据源的,一个没有。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
XtraReport1()
{
InitializeComponent();
}
//数据预览是有用
public
XtraReport1(DataSet ds)
//构造函数重载
{
InitializeComponent();
SetDataBind(ds);
}
private
void
SetDataBind(DataSet ds)
//绑定数据源
{
DataSource=ds;
this
.xrTableCell4.DataBindings.Add(
"Text"
, DataSource,
"test1"
);
this
.xrTableCell5.DataBindings.Add(
"Text"
, DataSource,
"test1"
);
}
|
1
|
填充数据代码如下:
|
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
|
private
void
simpleButton1_Click(
object
sender, System.EventArgs e)
{
XtraReport1 xrpt1=
new
XtraReport1(GetTempDataSet());
xrpt1.ShowPreviewDialog();
}
private
DataSet GetTempDataSet()
{
DataSet ds=
new
DataSet();
DataTable dt=
new
DataTable(
"tempTable"
);
dt.Columns.Add(
"test1"
,Type.GetType(
"System.String"
));
dt.Columns.Add(
"test"
,Type.GetType(
"System.String"
));
dt.Columns.Add(
"test3"
,Type.GetType(
"System.String"
));
dt.Columns.Add(
"test4"
,Type.GetType(
"System.String"
));
dt.Columns.Add(
"test5"
,Type.GetType(
"System.String"
));
dt.Columns.Add(
"test6"
,Type.GetType(
"System.String"
));
dt.Columns.Add(
"test7"
,Type.GetType(
"System.String"
));
DataRow tempRow;
for
(
int
i=0;i<7;i++)
//i:Row
{
tempRow=dt.NewRow();
tempRow[0]=i.ToString();
tempRow[1]=i.ToString();
tempRow[2]=i.ToString();
tempRow[3]=i.ToString();
tempRow[4]=i.ToString();
tempRow[5]=i.ToString();
tempRow[6]=i.ToString();
dt.Rows.Add(tempRow);
}
ds.Tables.Add(dt);
return
ds;
}
|
1
|
其中关于主从表的话,因为传进去的DaTaSet所以我们可以再外面把相应的关系指定好后加到DaTaSet。
|
1
|
下面给出主从表的代码:
|
1
2
3
4
5
6
7
8
9
|
DataColumn parentColumns;
DataColumn childColumns;
parentColumns = ds.Tables[
"Suppliers"
].Columns[
"SupplierID"
];
childColumns = ds.Tables[
"Products"
].Columns[
"SupplierID"
];
DataRelation dsdr1 =
new
DataRelation(
"fk_1"
, parentColumns, childColumns);
ds.Relations.Add(dsdr1);
DataRelation dsdr2 =
new
DataRelation(
"fk_2"
, ds.Tables[
"Products"
].Columns[
"ProductID"
], ds.Tables[
"OrderDetails"
].Columns[
"ProductID"
]);
ds.Relations.Add(dsdr2);
|
而模板中绑定数据的话跟上面单表是的大同小异,但是里面的绑定要记得引用的是你绑定关系的外键,也就是,如下代码:
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
|
public
XtraReport1(DataSet ds)
{
InitializeComponent();
SetDataBing(ds);
}
private
void
SetDataBing(DataSet ds)
{
DataMember =
"Suppliers"
;
DataSource = ds;
this
.cellCompanyName.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.CompanyName"
);
this
.sContadName.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.ContadName"
);
this
.sCountry.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.Country"
);
this
.sContactTitle.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.ContactTitle"
);
this
.sRegion.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.Region"
);
this
.sPhone.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.Phone"
);
this
.sCity.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.City"
);
this
.sFax.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.Fax"
);
this
.sPostalCode.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.PostalCode"
);
this
.sHomePage.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.HomePage"
);
this
.sAddress.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.Address"
);
this
.sCompanyName.DataBindings.Add(
"Text"
, DataSource,
"Suppliers.CompanyName"
);
DetailReport.DataMember =
"fk_1"
;
DetailReport.DataSource = DataSource;
this
.pProductName.DataBindings.Add(
"Text"
, DataSource,
"fk_1.ProductName"
);
this
.pProductID.DataBindings.Add(
"Text"
, DataSource,
"fk_1.ProductID"
);
this
.pCategory.DataBindings.Add(
"Text"
, DataSource,
"fk_1.CategoryID"
);
this
.pUnit.DataBindings.Add(
"Text"
, DataSource,
"fk_1.QuantityPerUnit"
);
this
.pUnitPrice.DataBindings.Add(
"Text"
, DataSource,
"fk_1.UnitPrice"
);
this
.pDiscontinued.DataBindings.Add(
"Text"
, DataSource,
"fk_1.Discontinued"
);
DetailReport1.DataMember =
"fk_1.fk_2"
;
DetailReport1.DataSource = DataSource;
this
.oOrderID.DataBindings.Add(
"Text"
, DataSource,
"fk_1.fk_2.OrderID"
);
this
.oQuantity.DataBindings.Add(
"Text"
, DataSource,
"fk_1.fk_2.Quantity"
);
this
.oDiscount.DataBindings.Add(
"Text"
, DataSource,
"fk_1.fk_2.Discount"
);
this
.oUnitPrice.DataBindings.Add(
"Text"
, DataSource,
"fk_1.fk_2.UnitPrice"
);
}
|
1
|
this
.pProductName.DataBindings.Add(
"Text"
, DataSource,
"fk_1.ProductName"
)以这句来说明,
"fk_1.ProductName"
|
1
|
这个就是子表的数据字段,这部是主从表动态绑定的关键,这样数据就会随主表数据变化而变化。
|