从主报表传递参数时,DevExpress XtraReport主 - 详细子报表异常

时间:2014-10-31 00:38:13

标签: c# exception devexpress subreport xtrareport

我正在研究由DevExpress 14.1.4制作子报告

我有XtraReport1,用Orders表绑定它 XtraReport2,使用OrderDetails表绑定它 XtraReport2按顺序具有参数oID,filer详细信息

在XtraReport1的beforePrint事件中,我确实喜欢这个教程:

private void xrLabel1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            int n = Convert.ToInt32(GetCurrentColumnValue("MaKhoa"));
            ((XtraReport2)((XRSubreport)sender).ReportSource).MK.Value = n;
        }

但它的抛出

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsFormsApplication1.exe

Additional information: Unable to cast object of type 'DevExpress.XtraReports.UI.XRLabel' to type 'DevExpress.XtraReports.UI.XRSubreport'.

1 个答案:

答案 0 :(得分:1)

<强>原因:
您已经处理了错误控制的BeforePrint事件。您正在处理xrLable1事件,因此发件人将是XRLabel并且您正在尝试将其强制转换为XRSubReport,因此您收到此错误:

private void xrLabel1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    int n = Convert.ToInt32(GetCurrentColumnValue("MaKhoa"));
    ((XtraReport2)((XRSubreport)sender).ReportSource).MK.Value = n; // you have error here
}

你应该处理SubReport BeforePrint事件来设置值,检查下面的代码片段:

using System.Drawing.Printing;
// ... 

private void xrSubreport1_BeforePrint(object sender, PrintEventArgs e) {
    ((XtraReport2)((XRSubreport)sender).ReportSource).CatID.Value = 
        Convert.ToInt32(GetCurrentColumnValue("CategoryID"));
}

我建议您按照以下文档链接正确制作Master-Detail Report:
Master-Detail Report
How to: Create a Master-Detail Report using Subreports
Lesson 3 - Create a Master-Detail Report