动态传递参数到DevExpress.XtraReports的小结

前两种方法和WinForm一样,可以传递参数、数组、实体对象、DataTable等
1. 采用构造函数
具体用法:
在Report中
public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
{
private int test1;

public Form1(int test1)
{
this.test1 = test1;
InitializeComponent();
}
}
调用Report
int test1 = 1;
XtraReport1 report = new XtraReport1(test1);
report.Show();

2.采用属性
具体用法:
在Report中
public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
{
public Form1()
{
InitializeComponent();
}
private int test1;
public int Test1
{
set { test1 = value; }
get { return test1; }
}
}
调用Report
XtraReport1 report = new XtraReport1();
report .Test1 = 1;
report.Show();

3.采用DataSet传递参数
在报表设计界面中,从工具栏数据中拉入DataSet到界面中,选择非类型化数据集,然后给拉入的DataSet添加Table和Column。报表界面的Field List中会自动加入刚添加进去的表和栏目,然后在拉动Field List栏中的Column到报表中,设计好後。在报表的代码中:
private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
this.DataSource = ds.Table[0];
}

我使用以上三种方法都没问题。

但我在允许用户修改报表设计
DevExpress.XtraReports.UI.XtraReport report = DevExpress.XtraReports.UI.XtraReport.FromFile(Application.StartupPath + "\\ReportTest.repx" );
report.ShowDesigner();
如果采用第1、2种方法,怎么也不行。后来只能变通,把要传递的数据保存在XML中,然后在Detail_BeforePrint事件中把XML文件中的数据读出来。

查看帮助说明如下:
in the assembly (represented by the .EXE or .DLL file) which produced the REPX file. Its path is also mentioned in the REPX file's header;

  1. in the current assembly where the FromFile method is called from;
  2. in the assemblies referenced by the current assembly.

If this class type is not found, then an instance of the XtraReport class is created.

Also, the saved state can be applied to the created report instance, if the loadState parameter is set to true.

等有空的时候使用反射试试,看能否让第1、2中传递参数的方法也可以实现用户自定义报表。

你可能感兴趣的:(UI,xml,WinForm)