【问题标题】:How to bind objects correctly with reportviewer?如何使用reportviewer正确绑定对象?
【发布时间】:2011-03-16 12:12:04
【问题描述】:

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

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

带有 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 集合,然后分别绑定它们,如下所示:

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);

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

【问题讨论】:

    标签: c# .net objectdatasource reportviewer2008


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2011-03-01
      • 2012-03-26
      • 1970-01-01
      • 2012-08-22
      • 2016-02-13
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      相关资源
      最近更新 更多