如何使用reportviewer正确绑定对象?

发布于 2024-10-22 13:05:17 字数 1875 浏览 5 评论 0原文

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

我试图从对象(标题)集合中绑定数据,其中每个对象都有一组子对象(行)。这怎么能做到呢?下面是我当前拥有的代码片段(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);

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

I've been studying report viewer recently and I have one problem I cannot resolve...

I'm trying to bind data from a collection of objects (headers) where each object has a collection of child objects (rows). How can this be done? Below are pieces of code that I currently have (somedata is a collection of header objects).

Windows form with ReportViewer control has following:

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

Header object:

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

Row object:

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

I created a report that has all the properties of ReportHeader and a list which should contain all the ReportRows but it doesn't seem work. The only solution was to make two separate collections, ReportHeader collection and ReportRow collection, and then bind them separately like below:

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

With this solution I would need to make some kind of relation between the collections..? So please help. (note that I simplified the objects a lot for my question)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-10-29 13:05:17

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

If i understand your question correctly. Could you just pass in a bool object isHeader and then in your visiability on your .rdlc text box do a function to =Fields!isHeader.value. If you have all your fields layered and hidden correctly you can have a header AND data in the same column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文