C# 如何使用C停止在ReportViewer中打印错误页#

C# 如何使用C停止在ReportViewer中打印错误页#,c#,reportviewer,C#,Reportviewer,我有一个主报告文件和一个子报告文件 主报告文件调用子报告文件 让我们先看看代码 private void CreatePDF(string fileName) { try { // Variables Warning[] warnings; string[] streamIds; string mimeType = string.Empty;

我有一个主报告文件和一个子报告文件

主报告文件调用子报告文件

让我们先看看代码

    private void CreatePDF(string fileName)
    {
        try
        {
            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            string _strGijunMonth = DateTime.Parse(GijunMonth).ToString("yyyyMM");
            byte[] bytes = null;

            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.LocalReport.DataSources.Clear();
            viewer.ProcessingMode = ProcessingMode.Local;
            viewer.LocalReport.ReportEmbeddedResource = "MasterReport.rdlc";
            viewer.LocalReport.EnableExternalImages = true;

            DataTable _dt = base.GetDataTable(
               "my_procedure"
               , _strMainNo
               );
            _intTotalPage = _dt.Rows.Count * 2;

            ReportDataSource _ds = new ReportDataSource();
            _ds.Value = _dt;
            _ds.Name = "SetData";
            viewer.LocalReport.DataSources.Add(_ds);

            // sub report event
            viewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

            // print
            viewer.RefreshReport();
            bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

            System.IO.FileStream newFile1 = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
            newFile1.Write(bytes, 0, bytes.Length);
            newFile1.Close();
        }
        catch
        {
            throw;
        }
    }

    void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
    {
        try
        {
            string MAINT_NO = e.Parameters["MAINT_NO"].Values[0];
            string _strGijunMonth = DateTime.Parse(GijunMonth).ToString("yyyyMM");

            // get sub report procedure
            DataSet _dsCust_Info = base.GetDataSet(
                "my_sub_procedure"
                , MAINT_NO
                , _strGijunMonth
                );

            ----> by somehow, it should throw error. If so, I should not print error page to pdf.
        }
        catch (Exception err)
        {
        }
    }
我的应用程序使用文件名参数调用“CreatePDF”方法

假设我必须打印到PDF 5页

调用LocalReport_SubreportProcessing事件时,某些子报表的数据中存在错误值。因此,我在LocalReport\u SubreportProcessing事件中抛出了一个错误

例如,当我说有5页,只有1、2、3和5页是可以的,第4页不应该打印为PDF

我想知道如何删除ReportViewer已经创建的PDF页面。 如您所见,LocalReport\u SubreportProcessing事件发生在创建PDF文件之后


有人知道如何解决此问题吗?

可能是reportViewer.CancelRendering(0); 当您检测到错误时