基于代码的区域报表非常灵活,如,可在同一个页面实现多个数据源(本例展现的是多个DataTable)绑定,其核心思想是区域报表的detail_Format事件中赋值的。
创建数据源DataSet(Form1.cs Line 37):
- privateDataSet CreateDataSet()
- {
- DataTable table1 =newDataTable("table1");
- table1.Columns.Add("name");
- table1.Columns.Add("id");
- table1.Rows.Add("Spread", 1);
- table1.Rows.Add("ComponentOne", 2);
- table1.Rows.Add("ActiveReports", 3);
- DataTable table2 =newDataTable("table2");
- table2.Columns.Add("desc");
- table2.Columns.Add("id");
- table2.Rows.Add("", 1);
- table2.Rows.Add("", 2);
- table2.Rows.Add("", 3);
- DataSet ds =newDataSet();
- ds.Tables.Add(table1);
- ds.Tables.Add(table2);
- return ds;
- }
添加<基于代码的区域报表>SectionReport2DataSet.cs,双击报表设计界面,在UI放置2个label和2个TextBox分别用于呈现2个数据源的数据,如下图所示:
在ReportStart事件中,通过添加代码来实现TextBox1(数据源一)的映射绑定:
- void SectionReport2DataSet_ReportStart(object sender, EventArgs e)
- {
- //DataSet1
- this.textBox1.DataField ="name";
- }
其次,在detail_Format中,实现TextBox2(数据源二)的赋值--通过TextBox1的值获得对应TextBox2的值。
- void detail_Format(object sender, EventArgs e)
- {
- //DataSet2textBox1
- this.textBox2.Value = FindName(this.textBox1.Value);
- }
最后,在Form中,加载区域报表:
- privatevoid ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SectionReport2DataSet sec =newSectionReport2DataSet();
- //DataMember
- sec.DataSource = CreateDataSet();
- sec.DataMember ="table1";
- this.viewer1.LoadDocument(sec);
- }
运行结果同我们预期一样:在区域报表Detail中,加载了2个不同的DataTable
源代码:VS2013 + C#.net + AR8
参考阅读: ActiveReports 区域报表中的事件介绍