This example shows the master-detail report created with the XRSubreport control and illustrates the following help topic: Create a Master-Detail Report with a Subreport.
Files to Review
- Form1.cs (VB: Form1.vb)
- MasterReportOne.cs (VB: MasterReportOne.vb)
- CategoryProductDataSource.cs (VB: CategoryProductDataSource.vb)
Documentation
More Examples
- Reporting for WinForms - How to Create a Report Bound to the SQL Data Source
- Bind a Report to an Object Data Source
- How to bind a report to a federated master-detail data source
- Use Subreports to Add a Chart
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dxSample {
public partial class Form1 : DevExpress.XtraBars.ToolbarForm.ToolbarForm {
public Form1() {
InitializeComponent();
}
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
barButtonItem2.Down = false;
var report = new MasterReport();
report.CreateDocument();
this.documentViewer1.DocumentSource = report;
}
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
barButtonItem1.Down = false;
var report = new MasterReportOne();
report.DataSource = CategoryProductDataSource.GetData();
report.CreateDocument();
this.documentViewer1.DocumentSource = report;
}
}
}
C#using DevExpress.XtraReports.UI;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
namespace dxSample {
public partial class MasterReportOne : DevExpress.XtraReports.UI.XtraReport {
public MasterReportOne() {
InitializeComponent();
}
private void xrSubreport1_BeforePrint(object sender, CancelEventArgs e) {
var category = GetCurrentRow() as Category;
((XRSubreport)sender).ReportSource.DataSource = category.Products;
}
}
}
C#using System.Collections.Generic;
public class Category {
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public List<Product> Products { get; set; }
}
public class Product {
public string ProductName { get; set; }
public double UnitPrice { get; set; }
}
public static class CategoryProductDataSource {
public static List<Category> GetData() {
return new List<Category>
{
new Category()
{
CategoryName = "Beverages",
Description = "Soft drinks, coffees, teas, beers, and ales",
Products = new List<Product>
{
new Product{ ProductName = "Chai", UnitPrice = 18 },
new Product{ ProductName = "Chang", UnitPrice = 19 },
new Product{ ProductName = "Guaraná Fantástica", UnitPrice = 4.5 }
},
},
new Category()
{
CategoryName = "Condiments",
Description = "Sweet and savory sauces, relishes, spreads, and seasonings",
Products = new List<Product>
{
new Product{ ProductName = "Aniseed Syrup", UnitPrice = 10 },
new Product{ ProductName = "Chef Anton's Cajun Seasoning", UnitPrice = 22 },
new Product{ ProductName = "Chef Anton's Gumbo Mix", UnitPrice = 21.35 },
new Product{ ProductName = "Grandma's Boysenberry Spread", UnitPrice = 25 }
},
},
new Category()
{
CategoryName = "Confections",
Description = "Desserts, candies, and sweet breads",
Products = new List<Product>
{
new Product{ ProductName = "Pavlova", UnitPrice = 17.45 },
new Product{ ProductName = "Teatime Chocolate Biscuits", UnitPrice = 9.2 },
new Product{ ProductName = "Sir Rodney's Marmalade", UnitPrice = 81 },
new Product{ ProductName = "Sir Rodney's Scones", UnitPrice = 10 },
new Product{ ProductName = "NuNuCa Nuß-Nougat-Creme", UnitPrice = 14 },
},
}
};
}
}