在C#中新建一个WinForm项目,并在工具栏中引用FastReport Studio的Com库,选择其中的预案控件,并将其拖放到窗口上。
在FastReport Studio的安装路径的例子程序中,将FrxDataTable复制到程序中并加入。程序代码如下:
1 private void Form1_Load(object sender, EventArgs e)
2 {
3 TfrxReportClass report = new TfrxReportClass();
4 //为报表指定模板文件
5 report.LoadReportFromFile(Application.StartupPath + "\\demo.fr3");
6
7 //此处的Class为报表中设计时所使用的数据集名称
8 FrxDataTable datatable = new FrxDataTable("class");
9
10 //设置列
11 datatable.Columns.Add("id", typeof(int));
12 datatable.Columns.Add("name", typeof(string));
13
14 string connString = "Data Source=master;Persist Security Info=True;User ID=l;pwd=1;Unicode=True";
15 OracleConnection conn = new OracleConnection(connString);
16 OracleCommand command = new OracleCommand("SELECT t.id,t.name FROM grade t", conn);
17 try
18 {
19 conn.Open();
20
21 //赋值
22 using (OracleDataReader rdr = command.ExecuteReader())
23 {
24 while (rdr.Read())
25 {
26 datatable.Rows.Add(new object[] { rdr.GetInt32(0), rdr.GetString(1) });
27 }
28 datatable.AcceptChanges();
29 }
30 }
31 finally
32 {
33 conn.Close();
34 }
35 //绑定
36 datatable.AssignToReport(true, report);
37 datatable.AssignToDataBand("MasterData1", report);
38
39 this.axTfrxPreviewX1.Report = report;
40 report.ShowReport();
41 }
42
2 {
3 TfrxReportClass report = new TfrxReportClass();
4 //为报表指定模板文件
5 report.LoadReportFromFile(Application.StartupPath + "\\demo.fr3");
6
7 //此处的Class为报表中设计时所使用的数据集名称
8 FrxDataTable datatable = new FrxDataTable("class");
9
10 //设置列
11 datatable.Columns.Add("id", typeof(int));
12 datatable.Columns.Add("name", typeof(string));
13
14 string connString = "Data Source=master;Persist Security Info=True;User ID=l;pwd=1;Unicode=True";
15 OracleConnection conn = new OracleConnection(connString);
16 OracleCommand command = new OracleCommand("SELECT t.id,t.name FROM grade t", conn);
17 try
18 {
19 conn.Open();
20
21 //赋值
22 using (OracleDataReader rdr = command.ExecuteReader())
23 {
24 while (rdr.Read())
25 {
26 datatable.Rows.Add(new object[] { rdr.GetInt32(0), rdr.GetString(1) });
27 }
28 datatable.AcceptChanges();
29 }
30 }
31 finally
32 {
33 conn.Close();
34 }
35 //绑定
36 datatable.AssignToReport(true, report);
37 datatable.AssignToDataBand("MasterData1", report);
38
39 this.axTfrxPreviewX1.Report = report;
40 report.ShowReport();
41 }
42
运行程序显示结果: