此文是紧接着Silverlight中动态绑定PageReport数据源(GrapeCity数据提供器)文章讲解如何动态绑定PageReport的数据源,此文中的方法是使用DataSet数据提供器将System.Data.DataSet作为PageReport的数据源。

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


指定应用程序使用的Silverlight版本,我们选择Silverlight 4,并创建一个新的Web项目



第二步:添加PageReport
在【PageReportDataSource_Silverlight_CSharp_2.Web】项目中添加一个PageReport,
添加项目对话框中我们选择【ActiveReports 7 Page Report】模板类型。新添加的PageReport默认为“固定页面布局报表(FPL)”,我们打开PageReport的设计视图,然后在VS的菜单中可以看到一个【Report】菜单项,此时,我们可以通过【Report】菜单中的【Convert to CPL Report】菜单项,将报表转换为“连续页面布局报表(CPL)”



第三步:添加虚拟数据源
打开PageReport1的设计界面,在VS菜单的View>Other Windows中打开Report Explorer 7



在Report Explorer 7窗口中,在DataSource节点中添加一个名为DataSetDataSource数据源,并且在类型下拉框中选择Dataset Provider



在DataSetDataSource数据源中添加以下两个数据集
Name:ProductsDataSet
Fields:ProductID、InStock、Price



Name:PersonDataSet
Fields:PersonID、FirstName、LastName、DateOfBirth



第四步:设计报表模板
在PageReport1中添加两个Table、第一个Table的DataSetName设置为ProductsDataSet,第二个Table的DataSetName设置为PersonDataSet



第五步:准备数据源
在PageReportDataSource_Silverlight_CSharp_2.Web中创建一个名为DataLayer的类来组织并返回数据源,代码如下:

internal sealed class DataLayer
    {
        private DataSet dataSetData;
        public DataLayer()
        {
            LoadDataToDataSet();
        }

        public DataSet DataSetData
        {
            get { return dataSetData; }
        }

        private void LoadDataToDataSet()
        {
            string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Reels.mdb";

            dataSetData = new DataSet();
            OleDbConnection conn = new OleDbConnection(connStr);
            OleDbCommand cmd = new OleDbCommand("", conn);
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = cmd;

            DataTable dt = new DataTable();
            dt.TableName = "Products";
            cmd.CommandText = "SELECT TOP 50 * From Product";
            adapter.Fill(dt);
            dataSetData.Tables.Add(dt);

            DataTable dt2 = new DataTable();
            dt2.TableName = "Person";
            cmd.CommandText = "SELECT TOP 50 * From Person";
            adapter.Fill(dt2);
            dataSetData.Tables.Add(dt2);
        }
    }

在PageReportDataSource_Silverlight_CSharp_2.Web中创建一个名为ReportService1的WebService返回报表内容:

[WebMethod]
        public Byte[] GetProductsReport()
        {
            System.IO.FileInfo rptPath = new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx"));
            //Create a report definition that loads an existing report.
            GrapeCity.ActiveReports.PageReport definition = new GrapeCity.ActiveReports.PageReport(rptPath);
            definition.ConfigurationProvider = new GrapeCity.ActiveReports.Configuration.DefaultConfigurationProvider();
            //Load the report definition into a new page document.
            GrapeCity.ActiveReports.Document.PageDocument runtime = new GrapeCity.ActiveReports.Document.PageDocument(definition);
            //Attach the runtime to an event. This line of code creates the event shell below.
            runtime.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(runtime_LocateDataSource);

            GrapeCity.ActiveReports.Export.Rdf.RdfRenderingExtension rdfe = new GrapeCity.ActiveReports.Export.Rdf.RdfRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider ms = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();

            runtime.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;
        }

        void runtime_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
        {
            DataLayer dl = new DataLayer();
            switch (args.DataSetName.ToUpper())
            {
                case "PRODUCTSDATASET":
                    args.Data = dl.DataSetData.Tables["Products"];
                    break;
                case "PERSONDATASET":
                    args.Data = dl.DataSetData.Tables["Person"];
                    break;
            }
        }

注意:要在PageReportDataSource_Silverlight_CSharp_2.Web中添加GrapeCity.ActiveReports.Export.Rdf.v7的引用

第六步:在Silverlight端加载报表
在PageReportDataSource_Silverlight_CSharp_2中添加ReportService1的引用



并给viewer1的Load事件添加以下后台代码:

private void viewer1_Loaded(object sender, RoutedEventArgs e)
        {
            // 从WebService加载报表
            ReportServiceReference.ReportService1SoapClient client = new ReportServiceReference.ReportService1SoapClient();
            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);
        }

运行结果:



源码下载:VS2010 + ActiveReports 7 V7.0.6158.0 + Silverlight 4

PageReportDataSource_Silverlight_CSharp_2.zip ( 3.22 M, 下载次数:5)