如何使用分组的自定义IEnumerable提供RDLC报告

时间:2015-08-06 22:56:23

标签: c# grouping rdlc dynamic-rdlc-generation igrouping

我想重复使用相同的RDLC来显示不同的表格,具体取决于格式字段。但是,当数据源已将IEnumerable<IGrouping<string, Row>>返回类型分组时,我的报告不会显示任何内容。

所以如果用户选择格式A,报告将显示按某些列分组的表格,如果用户选择格式B,则报告将显示按其他方式分组的表格

我的计划B是简单地将数据源作为Enumerable传递,然后在报告中按顺序排序,但我希望在代码中包含所有逻辑,而不是在RDLC中。

我使用自定义数据集来构建报告,如下所示:

    public byte[] GetReport(ReportFormat reportFormat)
    {
        MyReportDataSource myReportDataSource = GetMyReportDataSource(reportFormat);

        ReportDataSource dsDetails = GetDetailsDataSource(myReportDataSource);
        ReportDataSource dsRows = GetRowsDataSource(myReportDataSource);

        LocalReport localReport = new LocalReport();
        localReport.ReportPath = "pathToMyRdlc.rdlc";
        localReport.DataSources.Add(dsDetails);
        localReport.DataSources.Add(dsRows);

        Warning[] warnings;
        string encoding;
        string fileNameExtension;
        string[] streams;

        byte[] reportBytes = localReport.Render("pdf", null, PageCountMode.Actual,
                        out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

        return reportBytes;
    }

    private MyReportDataSource GetMyReportDataSource(ReportFormat reportFormat)
    {
        MyReportDataSource r = new MyReportDataSource();
        r.ReportFormat = ReportFormat.A;
        //some test rows
        Row row1 = new Row
        {
            Name = "Joe",
            LastName = "Doe",
            Age = 42
        };
        Row row2 = new Row
        {
            Name = "Jane",
            LastName = "Doe",
            Age = 26
        };
        Row row3 = new Row
        {
            Name = "Joe",
            LastName = "Smith",
            Age = 31
        };
        r.AddRow(row1);
        r.AddRow(row2);
        r.AddRow(row3);
        return r;
    }

    private ReportDataSource GetDetailsDataSource(MyReportDataSource myReportDataSource)
    {
        //ReportDataSource only accepts dataTable or IEnumerable, use IEnumerable but with single element as workaround
        List<MyReportDataSource> listSingleItem = new List<MyReportDataSource>();
        listSingleItem.Add(myReportDataSource);

        ReportDataSource detailsDataSource = new ReportDataSource("DSDetails", listSingleItem.AsEnumerable());
        return detailsDataSource;
    }

    private ReportDataSource GetDeclarationRowsDataSource(MyReportDataSource myReportDataSource)
    {
        ReportDataSource rowsDataSource = new ReportDataSource("DSRows", myDataSource.Rows);
        return rowsDataSource;
    }

所以基本上我传递给本地报告两个数据源,一个DSDetails用于详细信息(指定格式在哪里),另一个DSRows用于内部Enumerable,其中包含我希望在表格中显示的数据。这将是我的自定义数据源对象

public class DeclarationReportDataSource
{
    public ReportFormat ReportFormat { get; set; }

    private decimal _totalAge;
    public decimal TotalAge
    {
        get
        {
            return Math.Round(this._totalAge, 2);
        }
    }
    private List<Row> _Rows = new List<Row>();
    //HERE IS THE PROBLEM
    //This is the method that returns an IEnumerable ordered by
    //depending on the format
    //There is no problem to display data if I return an IEnumerable<Row>
    public IEnumerable<IGrouping<string, Row>> Rows
    {
        get
        {
            if (this.ReportFormat == ReportFormat.A)
            {
                return this._Rows.GroupBy(r => r.Name);
            }
            //else group by different field
            return this._Rows.GroupBy(r => r.LastName);
        }
    }

    public void AddRow(Row row)
    {
        this._Rows.Add(row);
        this._totalAge += row.Age;
    }
}

我对RDLC很新,所以任何关于如何做类似事情的指导或好文章都会非常感激。感谢

更新报表中的我的表格显示2个空行,因此代码中的分组工作正常但行不显示任何内容。所以我怀疑我错过了一些关于如何渲染分组行的概念。我会一直在调查。

0 个答案:

没有答案