首先新建一个报表项目,并设置报表纸张大小、页边距等信息;
根据需要,设计报表的布局;
完成布局设计后,绑定数据源。在此利用代码绑定数据源;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using BaseUI.BaseClass;
namespace LYWJMIS
{
public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
{
public XtraReport1()
{
InitializeComponent();
}
/// <summary>
/// 带参数的构造函数
/// </summary>
/// <param name="ds">要绑定的数据集</param>
public XtraReport1(DataSet ds): this()
{
if (!FormAssistant.IsDsNull(ds))
{
this.DataSource = ds;
this.DataMember = "Table1";
FillControlValue(ds);
}
}
private void FillControlValue(DataSet ds)
{
//为XRLable绑定数据集及对应的字段
this.txtDB0005A.DataBindings.Add("Text",ds,"DB0005A");
this.txtDB0336A.DataBindings.Add("Text",ds,"DB0336A");
this.txtDB0337A.DataBindings.Add("Text",ds,"DB0337A");
this.txtDB0339A.DataBindings.Add("Text",ds,"DB0339A");
this.txtDB0345A.DataBindings.Add("Text",ds,"DB0345A");
}
}
}
数据绑定后,调用报表,打印预览;
private void btnPrint_Click(object sender, EventArgs e)
{
XtraReport1 rep = new XtraReport1(dsSearch);
//设置纸张类型为自定义
rep.PaperKind = System.Drawing.Printing.PaperKind.Custom;
//设置纸张大小,单位:像素
double width = 302;
double height = 199;
rep.PageSize = new System.Drawing.Size((int)width, (int)height);
//打印预览
rep.ShowPreview();
}