XtraReport:无法使用分配给DataSource属性的对象

时间:2012-12-15 09:43:22

标签: c# .net devexpress datasource xtrareport

我有一个C#应用程序使用存储在数据库中的报表,对于特定报表,使用XPObject作为数据源( CptOperation类,代码在下面),我有这个错误消息在尝试打印或预览时:

  

分配给DataSource属性的对象不能用作   report的数据源,因为它没有实现任何受支持的数据源   接口。有关更多信息,请参阅   http://help.devexpress.com/#XtraReports/CustomDocument1179

以下是我用来打印报告的代码。

public static void PrintReport(string reportCode, object dataSource, string printerName)
{
    using (var uow = new UnitOfWork { ConnectionString = Content.GlobalInfo.ServerConnectionString })
    {
        var report = uow.FindObject<Content.Report>(new BinaryOperator("Code", reportCode));
        if (report == null)
        {
            XtraMessageBox.Show(String.Format("The report {0} is not found", reportCode),
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        var xtraReport = getXtraReportFromReport(report);
        xtraReport.DataSource = dataSource;

        if (!String.IsNullOrEmpty(printerName))
            xtraReport.Print(printerName);
        else
            xtraReport.Print();
    }
}

private static XtraReport getXtraReportFromReport(Content.Report report)
{
    XtraReport xtraReport;
    using (var writer = new StreamWriter(new MemoryStream()))
    {
        writer.Write(report.Content);
        writer.Flush();
        xtraReport = XtraReport.FromStream(writer.BaseStream, true);
    }
    return xtraReport;
}

这是我的对象持久性类“CptOperation”:

private CptTypeOperation cptTypeOperation;
public CptTypeOperation CptTypeOperation
{
    get { return cptTypeOperation; }
    set { SetPropertyValue<CptTypeOperation>("CptTypeOperation", ref cptTypeOperation, value); }
}

private int numero;
public int Numero
{
    get { return numero; }
    set { SetPropertyValue<int>("Numero", ref numero, value); }
}

private CptSession cptSession;
[Association("CptSession-CptOperation")]
public CptSession CptSession
{
    get { return cptSession; }
    set { SetPropertyValue<CptSession>("CptSession", ref cptSession, value); }
}

[Association("CptOperation-Piece")]
public XPCollection<Piece> Pieces
{
    get { return GetCollection<Piece>("Pieces"); }
}

[Association("CptOperation-Transact")]
public XPCollection<Transact> Transacts
{
    get { return GetCollection<Transact>("Transacts"); }
}

1 个答案:

答案 0 :(得分:4)

由于我发送的类型为XPObject的对象作为报表数据源,因此问题占用,但事实上,xtraReport数据源必须是IListIList<T>对象,例如,它可以是类型:XPCollection<CptOperation>List<CptOperation> ...