有时候,将设计器集成到业务系统中,为用户提供一些自定义的数据表,用户不需要了解如何底层的逻辑关系和后台代码,只需要选择几张关联的数据表,我们会根据用户的选择生成可供用户直接使用的数据集。本文第一部分主要讲解了,如何构造三种报表模板和,第二部分主要讲解了对于DataRelation类在动态绑定数据集之间的用法。首先需要构造LayoutBuilder类,该类主要构造三种类型的报表以及为它们添加数据集字段。
使用PageReport对象,并添加属性
public static void BuildRdlReportLayout(Design.Designer designer) { PageReport report = new PageReport(); report.Report.Body.Height = "5cm"; report.Report.Width = "20cm"; report.Load(new StringReader(report.ToRdlString())); //report = LayoutBuilder.AddDataSetDataSource(report);//Adding DataSources to the PageReport object report = LayoutBuilder.addDataSet(report); MemoryStream reportStream = LayoutBuilder.LoadReportToStream(report);//Loading the PageReport object to a stream reportStream.Position = 0; designer.LoadReport(XmlReader.Create(reportStream), DesignerReportType.Page); }
页面报表使用页面报表的构造字符串的方式来创建,否则会默认生成RDL报表
// 创建页面报表 public static void BuildPageReportLayout(Design.Designer designer) { PageReport report = new PageReport(); report.Load(new StringReader( @"<Report xmlns=""http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition""> <Body> <Height>27.9cm</Height> <ReportItems> <FixedPage Name=""FixedPage1""> <Pages> <Page /> </Pages> </FixedPage> </ReportItems> </Body> <BottomMargin>2.5cm</BottomMargin> <LeftMargin>2.5cm</LeftMargin> <PageHeight>27.9cm</PageHeight> <PageWidth>21.6cm</PageWidth> <RightMargin>2.5cm</RightMargin> <TopMargin>2.5cm</TopMargin> <Width>21.6cm</Width> </Report>")); report = LayoutBuilder.AddDataSetDataSource(report);//Adding DataSources to the PageReport object MemoryStream reportStream = LayoutBuilder.LoadReportToStream(report);//Loading the PageReport object to a stream reportStream.Position = 0; designer.LoadReport(XmlReader.Create(reportStream), DesignerReportType.Page); }
因为页面报表和RDL报表从代码级别而言,是相同的,都被视为PageReport,所以对于页面报表和RDL报表执行的类和接口也都相同。
添加数据集采用的是绑定预先设计的字段,因为在使用过程中用户只需要使用现成的数据来设计报表,所以我们需要为报表预先绑定好可以使用的字段,代码如下:
public static PageReport AddDataSetDataSource(PageReport report) { // create DataSource for the report DataSource dataSource = new DataSource(); dataSource.Name = "Reels Database"; dataSource.ConnectionProperties.DataProvider = "DATASET"; dataSource.ConnectionProperties.ConnectString = ""; //Create DataSet with specified query and load database fields to the DataSet DataSet dataSet = new DataSet(); Query query = new Query(); dataSet.Name = "Sample DataSet"; query.DataSourceName = "Reels Database"; query.CommandType = QueryCommandType.Text; query.CommandText = ""; dataSet.Query = query; String[] fieldsList = new String[] { "MoviedID", "Title", "YearReleased", "MPAA" }; foreach (string fieldName in fieldsList) { Field field = new Field(fieldName, fieldName, null); dataSet.Fields.Add(field); } //create report definition with specified DataSet and DataSource report.Report.DataSources.Add(dataSource); report.Report.DataSets.Add(dataSet); return report; }
区域报表的创建方式与页面报表类似,采用字符串读取的形式,并直接添加数据字段。
public static void BuildSectionReportLayout(Design.Designer designer) { // 空白区域报表 string rpx = "<?xml version=/"1.0/" encoding=/"utf-8/"?> <ActiveReportsLayout Version=/"3.2/" PrintWidth=/"9360/" DocumentName=/"ActiveReports Document/" ScriptLang=/"C#/" MasterReport=/"0/"> <StyleSheet> <Style Name=/"Normal/" Value=/"font-family: Arial; font-style: normal; text-decoration: none; font-weight: normal; font-size: 10pt; color: Black; text-align: left; vertical-align: top; ddo-char-set: 1/" /> <Style Name=/"Heading1/" Value=/"font-family: Arial; font-size: 16pt; font-style: normal; font-weight: bold/" /> <Style Name=/"Heading2/" Value=/"font-family: Times New Roman; font-size: 14pt; font-style: italic; font-weight: bold/" /> <Style Name=/"Heading3/" Value=/"font-family: Arial; font-size: 13pt; font-style: normal; font-weight: bold/" /> </StyleSheet> <Sections> <Section Type=/"PageHeader/" Name=/"PageHeader1/" Height=/"360/" BackColor=/"16777215/" /> <Section Type=/"Detail/" Name=/"Detail1/" Height=/"2880/" BackColor=/"16777215/" /> <Section Type=/"PageFooter/" Name=/"PageFooter1/" Height=/"360/" BackColor=/"16777215/" /> </Sections> <ReportComponentTray /> <PageSettings /> <Parameters /> </ActiveReportsLayout>"; // 区域报表数据源结构 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("Col1"); dt.Columns.Add("Col2"); dt.Columns.Add("Col3"); designer.Report = null; // 加载区域报表到设计器 designer.LoadReport(XmlReader.Create(LayoutBuilder.CovertStringToStream(rpx)), DesignerReportType.Section); // 设置区域报表数据源 SectionReport sr2 = designer.Report as SectionReport; sr2.DataSource = dt; }
LayoutBuilder.BuildRdlReportLayout(this.reportDesigner);
这样打开设计器之后,则会呈现带字段的数据集,当用户可以直接使用数据集字段,设计满足需求的报表了,当用户设计完成可能需要预览报表,这样就需要为报表返回实际的数据了。
首先在报表设计器中添加“预览”按钮
// 添加预览报表菜单 fileMenu.DropDownItems.Add(new ToolStripMenuItem("预览", null, new EventHandler(OnViewReport)));
实现预览方法,将当前设计的报表传给ReportViewer:
private void OnViewReport(object sender, EventArgs e) { ReportViewer viewer = new ReportViewer(); viewer.Report = reportDesigner.Report; viewer.ReportType = reportDesigner.ReportType; viewer.ShowDialog(); }
获取到保镖对象后,通过LocateDataSource来绑定字段:
switch (ReportType) { case DesignerReportType.Page: PageReport report1 = Report as PageReport; viewer1.LoadDocument(report1.Document); break; case DesignerReportType.Rdl: PageReport report2 = Report as PageReport; PageDocument reportD = new PageDocument(report2); reportD.LocateDataSource += new LocateDataSourceEventHandler(LoadDataSet); viewer1.LoadDocument(reportD); break;
其中我们在DataSet中使用了DataRelation 对象,用来创建Table之间的关系,但是AR对DataRelation的支持只限于父级数据的访问。
访问父数据表的字段时,字段的前缀应该为合适的数据表的关系名称,使用“.”进行分割。举例说明,有一个数据表OrderDetails作为子表关联到数据表Orders,两个数据表之间的关系名称为Orders_OrderDetails。可以使用下面的语法访问父数据表的字段OrderDate:Orders_OrderDetails.OrderDate
使用同样的语法可以访问嵌套多层的数据表字段。如,上例中的数据表Orders也存在父数据表Customers,关系名称为Customers_orders。命令行文本中指定数据表OrderDetails为主表,使用下面的语法访问父数据表的字段CustomerName:Customers_Orders.Orders_OrderDetails.CustomerName
注:当字段名称和关系使用相同名称时会发生错误,暂时不支持。
private void LoadDataSet(object sender, LocateDataSourceEventArgs args) { string constr = @"Provider=sqloledb; password=xA123456;data source=10.32.2.28;initial catalog=NWind_CHS;user id=sa;"; //此处修改为AR的测试数据库 // 创建DataSet DataSet myDataSet = new DataSet(); //string connStr = Properties.Resources.ConnectionString; OleDbConnection conn = new OleDbConnection(constr); DataTable[] myDataTables = new DataTable[3]; myDataTables[0] = new DataTable(Constants.SaleTableName); myDataTables[1] = new DataTable(Constants.SaleDetailsTableName); myDataTables[2] = new DataTable(Constants.CustomerTableName); //创建DataTable myDataSet.Tables.Add(myDataTables[0]); OleDbCommand cmd1 = new OleDbCommand(Constants.cmdText1, conn); OleDbDataAdapter oleAdapter1 = new OleDbDataAdapter(cmd1); oleAdapter1.Fill(myDataSet.Tables[0]); //为Table 添加数据 myDataSet.Tables.Add(myDataTables[1]); OleDbCommand cmd2 = new OleDbCommand(Constants.cmdText2, conn); OleDbDataAdapter oleAdapter2 = new OleDbDataAdapter(cmd2); oleAdapter2.Fill(myDataSet.Tables[1]); //为Table 添加数据 myDataSet.Tables.Add(myDataTables[2]); OleDbCommand cmd3 = new OleDbCommand(Constants.cmdText3, conn); OleDbDataAdapter oleAdapter3 = new OleDbDataAdapter(cmd3); oleAdapter3.Fill(myDataSet.Tables[2]); //创建 “DataRelation Customers_Orders” DataRelation Customers_Orders = new DataRelation("C_O",myDataSet.Tables[2].Columns["客户ID"], myDataSet.Tables[0].Columns["客户ID"] ); myDataSet.Relations.Add(Customers_Orders); //创建 “DataRelation ” DataRelation Orders_OrderDetails = new DataRelation("O_OD ", myDataSet.Tables[0].Columns["订单ID"], myDataSet.Tables[1].Columns["订单ID"]); myDataSet.Relations.Add(Orders_OrderDetails); //返回从表数据 args.Data = myDataSet.Tables[0];