本帖最后由 dof 于 2016-10-18 15:27 编辑
主要核心使用到了LocateDataSource事件,并根据数据集的名称决定加载什么数据,核心代码:
ASP.NET WebViewer 版本源码下载:
WinForms Viewer 版本源码下载:
- public partial class Index : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (IsPostBack)
- {
- return;
- }
- GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("RdlReport1.rdlx")));
- report.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
- WebViewer1.Report = report;
- }
- void Document_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
- {
- if (args.DataSetName == "ds_Category")
- {
- List<Category> list_Category = new List<Category>();
- list_Category.Add(new Category() { CategoryID = "1", Name = "类别1" });
- list_Category.Add(new Category() { CategoryID = "2", Name = "类别2" });
- list_Category.Add(new Category() { CategoryID = "3", Name = "类别3" });
- list_Category.Add(new Category() { CategoryID = "4", Name = "类别4" });
- args.Data = list_Category;
- }else if(args.DataSetName == "ds_Product")
- {
- string p1 = args.Report.Parameters[0].CurrentValue.ToString();
- List<Product> list_Product = new List<Product>();
- list_Product.Add(new Product() { CategoryID = "1", ProductID = "1", Name = "产品1", Price = 20 });
- list_Product.Add(new Product() { CategoryID = "1", ProductID = "2", Name = "产品2", Price = 30 });
- list_Product.Add(new Product() { CategoryID = "1", ProductID = "3", Name = "产品3", Price = 23 });
- list_Product.Add(new Product() { CategoryID = "1", ProductID = "4", Name = "产品4", Price = 28 });
- list_Product.Add(new Product() { CategoryID = "2", ProductID = "5", Name = "产品5", Price = 20 });
- list_Product.Add(new Product() { CategoryID = "2", ProductID = "6", Name = "产品6", Price = 30 });
- list_Product.Add(new Product() { CategoryID = "2", ProductID = "7", Name = "产品7", Price = 23 });
- list_Product.Add(new Product() { CategoryID = "2", ProductID = "8", Name = "产品8", Price = 28 });
- list_Product.Add(new Product() { CategoryID = "3", ProductID = "9", Name = "产品9", Price = 20 });
- list_Product.Add(new Product() { CategoryID = "3", ProductID = "10", Name = "产品10", Price = 30 });
- list_Product.Add(new Product() { CategoryID = "3", ProductID = "11", Name = "产品11", Price = 23 });
- list_Product.Add(new Product() { CategoryID = "3", ProductID = "12", Name = "产品12", Price = 28 });
- list_Product.Add(new Product() { CategoryID = "4", ProductID = "13", Name = "产品13", Price = 20 });
- list_Product.Add(new Product() { CategoryID = "4", ProductID = "14", Name = "产品14", Price = 30 });
- list_Product.Add(new Product() { CategoryID = "4", ProductID = "15", Name = "产品15", Price = 23 });
- list_Product.Add(new Product() { CategoryID = "4", ProductID = "16", Name = "产品16", Price = 28 });
- args.Data = list_Product.Where<Product>(p => p.CategoryID == p1);
- }
-
- }
- }
- public class Category
- {
- public string CategoryID { get; set; }
- public string Name { get; set; }
- }
- public class Product
- {
- public string CategoryID { get; set; }
- public string ProductID{get;set;}
- public string Name{get;set;}
- public float Price{get;set;}
- }
复制代码
|