在Silverlight中动态绑定PageReport的数据源

ActiveReports 7中引入了一种新的报表模型——PageReport(页面布局报表),这种报表模型又细分了两种具体显示形式:

发布于 2012/11/15 00:00

ActiveReports 7中引入了一种新的报表模型——PageReport(页面布局报表),这种报表模型又细分了两种具体显示形式:
o    固定页面布局报表模型(FPL)是ActiveReports 7中首创的一种 .NET报表模型,通过这种模型可以非常方便地设计出拥有复杂格式的报表模板。您只需定义好页面大小,然后以一种可视化的方式添加需要的控件并设置数据填充方式,剩下的工作将由报表引擎自动完成。
o    连续页面布局报表模型(CPL)主要通过数据区域来控制报表的布局,并能自动实现数据分页显示。这种报表模型非常适合于在同一个报表中显示多个数据集数据的需求,而且不必精细的控制数据在页面中的显示位置。连续页面布局报表还允许用户通过折叠/ 展开的方式来隐藏/显示报表内容。

下面就来看看在Silverlight平台中如果动态绑定PageReport数据源,本文中创建的报表选用的是连续页面布局模型(CPL)。

第一步:创建一个Silverlight项目
在VS2010中创建一个名为【PageReportDataSource_Silverlight_CSharp】的Silverlight应用程序


切换到ReportService.asmx的代码视图,并添加以下代码:
 
[WebMethod]
        public Byte[] GetProductsReport()
        {
            // 创建一个空白页面报表
            GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport();

            // 加载报表布局
            rpt.Load(new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx")));

            // 创建并设置数据源
            GrapeCity.ActiveReports.PageReportModel.DataSource myDataSource = new GrapeCity.ActiveReports.PageReportModel.DataSource();
            myDataSource.Name = "DataSource1";
            myDataSource.ConnectionProperties.DataProvider = "OLEDB";
            myDataSource.ConnectionProperties.ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Reels.mdb";

            // 设置数据集
            GrapeCity.ActiveReports.PageReportModel.DataSet myDataSet = new GrapeCity.ActiveReports.PageReportModel.DataSet();
            GrapeCity.ActiveReports.PageReportModel.Query myQuery = new GrapeCity.ActiveReports.PageReportModel.Query();
            myDataSet.Name = "DataSet1";
            myQuery.DataSourceName = "DataSource1";
            myQuery.CommandType = GrapeCity.ActiveReports.PageReportModel.QueryCommandType.Text;
            myQuery.CommandText = GrapeCity.ActiveReports.Expressions.ExpressionInfo.FromString("SELECT TOP 50 * FROM Product");
            myDataSet.Query = myQuery;

            // 添加字段
            GrapeCity.ActiveReports.PageReportModel.Field _field = new
            GrapeCity.ActiveReports.PageReportModel.Field("ProductID", "ProductID", null);
            myDataSet.Fields.Add(_field);
            _field = new GrapeCity.ActiveReports.PageReportModel.Field("InStock", "InStock", null);
            myDataSet.Fields.Add(_field);
            _field = new GrapeCity.ActiveReports.PageReportModel.Field("Price", "Price", null);
            myDataSet.Fields.Add(_field);

            // 将数据源和数据集绑定到报表中
            rpt.Report.DataSources.Add(myDataSource);
            rpt.Report.DataSets.Add(myDataSet);

            // 保存Rdf格式的报表
            GrapeCity.ActiveReports.Export.Rdf.RdfRenderingExtension rdfe = new GrapeCity.ActiveReports.Export.Rdf.RdfRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider ms = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();

            rpt.Run();
            rpt.Document.Render(rdfe, ms);

            GrapeCity.ActiveReports.Document.SectionDocument doc_rdf = new GrapeCity.ActiveReports.Document.SectionDocument();
            doc_rdf.Load(ms.GetPrimaryStream().OpenStream() as System.IO.MemoryStream);

            return doc_rdf.Content;
        }
复制代码
第四步:在Silverlight中浏览报表内容
切换到【PageReportDataSource_Silverlight_CSharp】工程中,打开“MainPage.xaml”的设计视图,此时在VS工具箱的“ActiveReports 7”分类下可以看到一个Viewer的控件,将该控件添加到“MainPage.xaml”中


完成以上操作之后,切换到“MainPage.xaml”的代码视图,添加Viewer.Loaded事件的代码:
 
private void viewer1_Loaded(object sender, RoutedEventArgs e)
        {
            // 从WebService加载报表
            ReportServiceReference.ReportServiceSoapClient client = new ReportServiceReference.ReportServiceSoapClient();
            client.GetProductsReportAsync();
            client.GetProductsReportCompleted += new EventHandler<ReportServiceReference.GetProductsReportCompletedEventArgs>(client_GetProductsReportCompleted);            
        }

        void client_GetProductsReportCompleted(object sender, ReportServiceReference.GetProductsReportCompletedEventArgs e)
        {
            // 显示报表
            System.IO.MemoryStream ms = new System.IO.MemoryStream(e.Result);
            GrapeCity.Viewer.Common.StreamDocumentLoader loader = new GrapeCity.Viewer.Common.StreamDocumentLoader(ms, GrapeCity.Viewer.Common.DocumentFormat.Rdf);
            viewer1.LoadDocument(loader);
        }
复制代码
运行工程,我们可以得到以下结果:
(1.19 M, 下载次数:13)

关于葡萄城

葡萄城是专业的软件开发技术和低代码平台提供商,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。葡萄城开发技术始于1980年,40余年来始终聚焦软件开发技术,有深厚的技术积累和丰富的产品线。是业界能够同时赋能软件开发和低代码开发的企业。凭借过硬的产品能力、活跃的开发者社区和丰富的伙伴生态,与超过3000家合作伙伴紧密合作,产品广泛应用于信息和软件服务、制造、交通运输、建筑、金融、能源、教育、公共管理等支柱产业。

推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态