如何在xtrareport devexpress中为子报表添加xml代码?

时间:2014-08-27 10:52:43

标签: devexpress xtrareport

我创建了一个主报告文件。然后我创建了子报告文件。有没有办法将子报表文件的xml代码作为报表源?

1 个答案:

答案 0 :(得分:0)

覆盖OnBeforePrint方法并浏览XtraReportBase.Controls属性树以查找XRSubreport。如上所述here,您可以使用DataSet及其DataSet.ReadXml方法:

protected override void OnBeforePrint(PrintEventArgs e)
{
    base.OnBeforePrint(e);

    //Get your xml here
    var dataSet = new DataSet();

    using (var reader = new StringReader(xml))
        dataSet.ReadXml(reader);

    SetSubReportXML(this, dataSet);
}

private void SetSubReportXML(XRControl xrControl, DataSet dataSet)
{
    foreach (XRControl xrControlChild in xrControl.Controls)
    {
        var subReport = xrControlChild as XRSubreport;

        if (subReport != null)
        {
            //Set your xml here
            subReport.ReportSource.DataSource = dataSet;
            subReport.ReportSource.DataMember = this.DataMember;

            SetSubReportXML(subReport.ReportSource, dataSet);
        }

        SetSubReportXML(xrControlChild, dataSet);
    }
}