以 AdventureWorks2019 Person.Address Table 資料為主,把同一個城市內的地址群組在一起來紀錄,子報表設計可以參考這三篇筆記
namespace MultiColumnSubreport
{
// 故意弄兩個 class 來呈現資料,不要都用 Address
public class MasterReportModel
{
public string City { get; set; }
}
public class DetailReportModel
{
public string City { get; set; }
public string Address { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var dbContext = new AdventureWorks2019Entities();
var source = dbContext.Address
.Where(x => x.City == "Perth")
.OrderBy(x => x.City)
.ThenBy(x => x.AddressLine1)
.ToList();
var masterSource = source.GroupBy(g => g.City).Select(g => new MasterReportModel() { City = g.Key }).ToList();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(nameof(MasterReportModel), masterSource));
reportViewer1.LocalReport.SubreportProcessing += (senderInner, eInner) =>
{
string city = eInner.Parameters[nameof(DetailReportModel.City)].Values[0]?.ToString();
var detailSource = source
.Where(w => w.City == city)
.Select(s => new DetailReportModel()
{
City = s.City,
Address = s.AddressLine1
})
.OrderBy(x => x.Address)
.ToList();
eInner.DataSources.Add(new ReportDataSource(nameof(DetailReportModel), detailSource));
};
reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
reportViewer1.RefreshReport();
}
}
}
正常的一頁兩欄位的多欄位報表
清單搭配子報表來呈現多欄位,變成一頁一欄,總共兩頁
原本擔心多欄位設計在單一群組有第二頁情況下,子報表無法呈現,一整個多慮,連正常多欄效果都出不來,完全無法使用
沒有留言:
張貼留言