gpt4 book ai didi

c# - 如何使用reportviewer正确绑定(bind)对象?

转载 作者:行者123 更新时间:2023-11-30 18:42:47 26 4
gpt4 key购买 nike

我最近一直在研究报表查看器,但我有一个问题无法解决...

我正在尝试从一组对象(标题)绑定(bind)数据,其中每个对象都有一组子对象(行)。如何才能做到这一点?以下是我目前拥有的代码片段(somedata 是 header 对象的集合)。

带有 ReportViewer 控件的 Windows 窗体具有以下内容:

reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.LoadReportDefinition(GetReport());
reportViewer1.LocalReport.DataSources.Clear();
var dataSourcesNames = GetDataSourceNames();
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
reportViewer1.RefreshReport();

标题对象:

public class ReportHeader{
readonly string id;
readonly List<ReportRow> rows;

public ReportData(Header h) {
this.id = h.Id;
rows = new List<ReportRow>();
foreach(RowObject o in h.Rows){
rows.Add(new ReportRow(o));
}
}

public string Id { get { return id; } }
public List<ReportRow> Rows { get { return rows;} }
}

行对象:

public class ReportRow{
readonly decimal sum;
readonly string type;
readonly string code;

public ReportDataRow(RowObject r) {
sum = r.Sum;
type = r.Type;
code = r.Code;
}

public decimal Sum { get { return sum; } }
public string Type { get { return type; } }
public string Code { get { return code; } }
}

我创建了一个具有 ReportHeader 的所有属性的报表和一个应该包含所有 ReportRows 的列表,但它似乎不起作用。唯一的解决办法是制作两个单独的集合,ReportHeader 集合和 ReportRow 集合,然后像下面这样分别绑定(bind)它们:

var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
var rowSource = new ReportDataSource(dataSourcesNames[1], somedata.Rows);
reportViewer1.LocalReport.DataSources.Add(rowSource);

使用此解决方案,我需要在集合之间建立某种关系..?所以请帮助。 (请注意,我为我的问题简化了很多对象)

最佳答案

如果我正确理解你的问题。你能不能只传入一个 bool 对象 isHeader,然后在你的 .rdlc 文本框的可见性中执行一个函数 =Fields!isHeader.value。如果您将所有字段正确分层和隐藏,则可以在同一列中包含标题和数据。

关于c# - 如何使用reportviewer正确绑定(bind)对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5325071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com