コード例 #1
0
        private void preview()
        {
            string reportFile = "";
            try
            {
                if (this.splitContainerControl1.Panel2.Controls.Count > 0 && this.treeList1.Selection[0].Tag != null)
                {
                    IReportFilter filter = (IReportFilter)this.splitContainerControl1.Panel2.Controls[0];

                    //Kiểm tra lỗi
                    Error.ClearErrors();
                    filter.ValidateFilter(Error);
                    if (Error.HasErrors) return;

                    #region Khởi tạo Main DataSet
                    DataSet ds = null;
                    ds = filter.getDataSet();
                    if (showEmptyReport==false)
                    {
                        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                        {
                            HelpMsgBox.ShowNotificationMessage("Không tìm thấy dữ liệu báo cáo.");
                            return;
                        }
                    }
                    ReportInfo info = (ReportInfo)this.treeList1.Selection[0].Tag;
                    ParameterFields Params = null;
                    ReportClass Report = null;
                    if (this.splitContainerControl1.Panel2.Controls[0] is IDynSheetReportFilter)
                    {
                        string Title = null;
                        string SubTitle = null;
                        string[] FieldNames = null;
                        string[] Captions = null;
                        int[] Widths = null;
                        bool IsVertical = false;
                        //Alignment[] aligns = null;

                        ((IDynSheetReportFilter)this.splitContainerControl1.Panel2.Controls[0]).GetParam(
                            out Title, out SubTitle, out FieldNames,
                            out Captions, out Widths, out IsVertical);

                        if (IsVertical)
                        {
                            Report = new VSheetReport();
                        }
                        else
                        {
                            Report = new HSheetReport();
                        }

                        DynamicSheetReport.ToSheetReport(Report, out Params, ds,
                            FieldNames,
                            Captions,
                            Widths, Title, SubTitle);

                    }
                    #endregion
                    FrameworkParams.wait = new WaitingMsg();

                    #region Thêm 1 Viewer
                    DevExpress.XtraTab.XtraTabPage page = null;
                    bool usingXtraReport = info.ReportFile.StartsWith("EMBXTRA");
                    PLCrystalReportViewer view = null;
                    PLXtraPreview xtraview = null;
                    Control viewCtrl = null;
                    if (usingXtraReport)
                    {
                        xtraview = new PLXtraPreview();
                        viewCtrl = xtraview;
                    }
                    else
                    {
                        view = new PLCrystalReportViewer();
                        view._SetSupportRefresh(false);
                        viewCtrl = view;
                    }
                    lock(AddPageFlag){
                        if (AddPageFlag.Value == true)
                        {
                            page = new DevExpress.XtraTab.XtraTabPage();
                            page.Text = this.treeList1.Selection[0].GetValue(0).ToString();
                            page.Controls.Add(viewCtrl);
                            AddPageFlag.Value = false;

                            this.xtraTabControl1.TabPages.Add(page);
                            page.Tag = this.splitContainerControl1.Panel2.Controls[0];
                            this.xtraTabControl1.SelectedTabPage = page;
                        }
                        else
                        {
                            page = this.xtraTabControl1.SelectedTabPage;
                            page.Controls.Clear();
                            page.Controls.Add(viewCtrl);
                        }
                    }
                    viewCtrl.Dock = DockStyle.Fill;
                    //DevExpress.XtraTab.XtraTabPage page = new DevExpress.XtraTab.XtraTabPage();
                    //page.Text = this.treeList1.Selection[0].GetValue(0).ToString();
                    //PLCrystalReportViewer view = new PLCrystalReportViewer();
                    //page.Controls.Add(view);

                    //view.Dock = DockStyle.Fill;
                    #endregion
                    XtraReport xtraReport = null;
                    if (this.splitContainerControl1.Panel2.Controls[0] is IDynSheetReportFilter)
                    {
                        #region Phiên bản 14
                        //view._I.ParameterFieldInfo = Params;
                        //view._SetSupportRefresh(false);
                        ////EnableParameterPrompting
                        //view._I.ReportSource = Report;
                        #endregion
                        #region Phiên bản 10
                        view.ParameterFieldInfo = Params;
                        view._SetSupportRefresh(false);
                        //EnableParameterPrompting
                        view.ReportSource = Report;
                        #endregion
                    }
                    else {
                        #region Lấy 1 ReportDocument

                        if (this.splitContainerControl1.Panel2.Controls[0] is IAdvanceReportFilter)
                            info.ReportFile = ((IAdvanceReportFilter)this.splitContainerControl1.Panel2.Controls[0]).getReportFile();
                        reportFile = info.ReportFile;
                        ReportDocument report = new ReportDocument();

                        if (reportFile.StartsWith("EMBXTRA"))
                        {
                            reportFile = reportFile.Substring(7);
                            try { xtraReport = (DevExpress.XtraReports.UI.XtraReport)GenerateClass.initObject(reportFile); }
                            catch { }
                        }
                        else if (reportFile.StartsWith("EMB"))
                        {
                            reportFile = reportFile.Substring(3);
                            try { report = (ReportDocument)GenerateClass.initObject(reportFile); }
                            catch { }
                        }
                        else if (reportFile.StartsWith("PATH"))
                        {
                            reportFile = reportFile.Substring(4);
                            report.Load(reportFile);
                        }
                        else //if (reportFile.StartsWith("\\report"))
                        { //Load theo dạng file
                            reportFile = RadParams.RUNTIME_PATH + reportFile;
                            report.Load(reportFile);
                        }

                        #endregion

                        #region Gán DataSet
                        if (usingXtraReport == false)
                        {
                            view._DSList = new List<DataSet>();
                            //Set DataSet cho Main Report
                            view._DSList.Add(ds);
                            for (int i = 0; i < ds.Tables.Count; i++)
                            {
                                report.Database.Tables[i].SetDataSource(ds.Tables[i]);
                            }
                            //Set DataSet cho SubReport
                            DataSet[] dsArray = filter.getSubReports();
                            if (dsArray != null)
                            {
                                for (int i = 0; i < dsArray.Length; i++)
                                {
                                    report.Subreports[i].SetDataSource(dsArray[i]);
                                    view._DSList.Add(dsArray[i]);
                                }
                            }
                            #region Parameter
                            Dictionary<string, object> dic = filter.GetParamFieldValue();
                            foreach (CrystalDecisions.Shared.ParameterField pa in report.ParameterFields)
                            {
                                if (dic.ContainsKey(pa.Name))
                                {
                                    report.SetParameterValue(pa.Name, dic[pa.Name]);
                                }
                            }
                            if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
                            //view._SetSupportRefresh(true);
                            //view._I.ReportSource = report;//phiên bản 12
                            view.ReportSource = report;//phiên bản 10
                            #endregion

                            report.RefreshReport += new EventHandler(report_RefreshReport);
                        }
                        else//XtraReport
                        {
                            xtraReport.DataSource = ds;
                            //Dạng có Main Report + Sub Report
                            DataSet[] dsArray = filter.getSubReports();
                            if (dsArray != null)
                            {
                                List<XRSubreport> subreport = new List<XRSubreport>();
                                foreach (Band band in xtraReport.Bands)//getBands(xtraReport))
                                {
                                    foreach (XRControl xrControl in band.Controls)
                                    {
                                        if (xrControl is XRSubreport)
                                        {
                                            subreport.Add((XRSubreport)xrControl);
                                        }
                                    }
                                }

                                //for (int i = 0; i < dsArray.Length; i++)
                                //{
                                //    subreport[i].ReportSource.DataSource = dsArray[i];
                                //}
                                //duchs: gan dataset cho datasource cua subreport dua vao ten bang
                                for (int i = 0; i < dsArray.Length; i++)
                                {
                                    foreach (XRSubreport itemreport in subreport)
                                    {
                                        try
                                        {
                                            //DataSet ds=(DataSet)itemreport.DataSource;
                                            if (itemreport.ReportSource.DataMember.ToUpper().Equals(dsArray[i].Tables[0].TableName))
                                            {
                                                itemreport.ReportSource.DataSource = dsArray[i].Tables[0];
                                                break;
                                            }
                                        }
                                        catch { }
                                    }
                                }
                            }
                            #region Parameter
                            Dictionary<string, object> dic = filter.GetParamFieldValue();
                            foreach (DevExpress.XtraReports.Parameters.Parameter par in xtraReport.Parameters)
                            {
                                if (dic.ContainsKey(par.Name))
                                {
                                    xtraReport.Parameters[par.Name].Value = dic[par.Name];
                                }
                            }
                            xtraReport.PrintingSystem.SetCommandVisibility(DevExpress.XtraPrinting.PrintingSystemCommand.ClosePreview, DevExpress.XtraPrinting.CommandVisibility.None);
                            //ẩn phần nhập Parameters
                            xtraReport.RequestParameters = false;
                            xtraview.PrintingSystem = xtraReport.PrintingSystem;
                            xtraReport.CreateDocument();
                            //ẩn nút Parameters trên toolbar
                            xtraReport.PrintingSystem.SetCommandVisibility(DevExpress.XtraPrinting.PrintingSystemCommand.Parameters, DevExpress.XtraPrinting.CommandVisibility.None);
                            //MessageBox.Show(view.PrintingSystem.Pages.Count.ToString());
                            #endregion
                        }
                        #endregion

                    }

                    if (FrameworkParams.isFax == true)
                    {
                        frmFax frmFax = new frmFax(info.Caption, FrameworkParams.TEMP_FOLDER + @"\" + "$faxdoc.pdf", "Thông tin người nhận", "Điện thoại");
                        view.frmFax = frmFax;
                    }

                    //this.xtraTabControl1.TabPages.Add(page);
                    //page.Tag = this.splitContainerControl1.Panel2.Controls[0];

                    //this.xtraTabControl1.SelectedTabPage = page;

                }
                //if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
            }
            catch(Exception e){
                if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
                HelpMsgBox.ShowNotificationMessage("Tập tin " + reportFile + " không tồn tại.\nVui lòng liên hệ Công ty P R O T O C O L.");
                PLException.AddException(e);
            }
            finally {
                if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
            }
        }
コード例 #2
0
ファイル: frmBaseReport.cs プロジェクト: khanhdtn/my-fw-win
        private void preview()
        {
            string reportFile = "";
            try
            {
                if (this.splitContainerControl1.Panel2.Controls.Count > 0 && this.treeList1.Selection[0].Tag != null)
                {
                    IReportFilter filter = (IReportFilter)this.splitContainerControl1.Panel2.Controls[0];

                    //Kiểm tra lỗi
                    Error.ClearErrors();
                    filter.ValidateFilter(Error);
                    if (Error.HasErrors) return;

                    #region Khởi tạo Main DataSet
                    DataSet ds = null;
                    ds = filter.getDataSet();
                    if (showEmptyReport == false)
                    {
                        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                        {
                            HelpMsgBox.ShowNotificationMessage("Không tìm thấy dữ liệu báo cáo.");
                            return;
                        }
                    }
                    ReportInfo info = (ReportInfo)this.treeList1.Selection[0].Tag;
                    ParameterFields Params = null;
                    ReportClass Report = null;
                    if (this.splitContainerControl1.Panel2.Controls[0] is IDynSheetReportFilter)
                    {
                        string Title = null;
                        string SubTitle = null;
                        string[] FieldNames = null;
                        string[] Captions = null;
                        int[] Widths = null;
                        bool IsVertical = false;
                        //Alignment[] aligns = null;

                        ((IDynSheetReportFilter)this.splitContainerControl1.Panel2.Controls[0]).GetParam(
                            out Title, out SubTitle, out FieldNames,
                            out Captions, out Widths, out IsVertical);

                        if (IsVertical)
                        {
                            Report = new VSheetReport();
                        }
                        else
                        {
                            Report = new HSheetReport();
                        }

                        DynamicSheetReport.ToSheetReport(Report, out Params, ds,
                            FieldNames,
                            Captions,
                            Widths, Title, SubTitle);

                    }
                    #endregion
                    FrameworkParams.wait = new WaitingMsg();

                    #region Thêm 1 Viewer
                    DevExpress.XtraTab.XtraTabPage page = null;
                    PLCrystalReportViewer view = new PLCrystalReportViewer();
                    view._SetSupportRefresh(false);

                    lock(AddPageFlag){
                        if (AddPageFlag.Value == true)
                        {
                            page = new DevExpress.XtraTab.XtraTabPage();
                            page.Text = this.treeList1.Selection[0].GetValue(0).ToString();
                            page.Controls.Add(view);
                            AddPageFlag.Value = false;

                            this.xtraTabControl1.TabPages.Add(page);
                            page.Tag = this.splitContainerControl1.Panel2.Controls[0];
                            this.xtraTabControl1.SelectedTabPage = page;
                        }
                        else
                        {
                            page = this.xtraTabControl1.SelectedTabPage;
                            page.Controls.Clear();
                            page.Controls.Add(view);
                        }
                    }
                    view.Dock = DockStyle.Fill;
                    //DevExpress.XtraTab.XtraTabPage page = new DevExpress.XtraTab.XtraTabPage();
                    //page.Text = this.treeList1.Selection[0].GetValue(0).ToString();
                    //PLCrystalReportViewer view = new PLCrystalReportViewer();
                    //page.Controls.Add(view);

                    //view.Dock = DockStyle.Fill;
                    #endregion

                    if (this.splitContainerControl1.Panel2.Controls[0] is IDynSheetReportFilter)
                    {
                        //#region Phiên bản 12
                        //view._I.ParameterFieldInfo = Params;
                        //view._SetSupportRefresh(false);
                        ////EnableParameterPrompting
                        //view._I.ReportSource = Report;
                        //#endregion
                        #region Phiên bản 10
                        view.ParameterFieldInfo = Params;
                        view._SetSupportRefresh(false);
                        //EnableParameterPrompting
                        view.ReportSource = Report;
                        #endregion
                    }
                    else {
                        #region Lấy 1 ReportDocument
                        ReportDocument report = new ReportDocument();
                        if (this.splitContainerControl1.Panel2.Controls[0] is IAdvanceReportFilter)
                            info.ReportFile = ((IAdvanceReportFilter)this.splitContainerControl1.Panel2.Controls[0]).getReportFile();
                        reportFile = info.ReportFile;
                        if (reportFile.StartsWith("EMB"))
                        {
                            reportFile = reportFile.Substring(3);
                            try { report = (ReportDocument)GenerateClass.initObject(reportFile); }
                            catch { }
                        }
                        else if (reportFile.StartsWith("PATH"))
                        {
                            reportFile = reportFile.Substring(4);
                            report.Load(reportFile);
                        }
                        else //if (reportFile.StartsWith("\\report"))
                        { //Load theo dạng file
                            reportFile = RadParams.RUNTIME_PATH + reportFile;
                            report.Load(reportFile);
                        }

                        #endregion

                        #region Gán DataSet
                        //2008
                        view._DSList = new List<DataSet>();
                        ////Set DataSet cho Main Report
                        view._DSList.Add(ds);
                        for (int i = 0; i < ds.Tables.Count; i++)
                        {
                            report.Database.Tables[i].SetDataSource(ds.Tables[i]);
                        }

                        //Set DataSet cho SubReport
                        DataSet[] dsArray = filter.getSubReports();
                        if(dsArray!=null){
                            for (int i = 0; i < dsArray.Length; i++)
                            {
                                report.Subreports[i].SetDataSource(dsArray[i]);
                                view._DSList.Add(dsArray[i]);
                            }
                        }
                        #endregion

                        #region Parameter
                        Dictionary<string, object> dic = filter.GetParamFieldValue();
                        foreach (CrystalDecisions.Shared.ParameterField pa in report.ParameterFields)
                        {
                            if (dic.ContainsKey(pa.Name))
                            {
                                report.SetParameterValue(pa.Name, dic[pa.Name]);
                            }
                        }
                        //view._SetSupportRefresh(true);
                        if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();

                        //view._I.ReportSource = report;//phiên bản 12
                        view.ReportSource = report;//phiên bản 10
                        #endregion

                        report.RefreshReport += new EventHandler(report_RefreshReport);
                    }

                    if (FrameworkParams.isFax == true)
                    {
                        frmFax frmFax = new frmFax(info.Caption, FrameworkParams.TEMP_FOLDER + @"\" + "$faxdoc.pdf", "Thông tin người nhận", "Điện thoại");
                        view.frmFax = frmFax;
                    }

                    //this.xtraTabControl1.TabPages.Add(page);
                    //page.Tag = this.splitContainerControl1.Panel2.Controls[0];

                    //this.xtraTabControl1.SelectedTabPage = page;

                }
                //if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
            }
            catch(Exception e){
                if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
                //HelpMsgBox.ShowNotificationMessage("Tập tin " + reportFile + " không tồn tại.\nVui lòng liên hệ Công ty P R O T O C O L.");
                HelpMsgBox.ShowNotificationMessage("Quá trình tạo báo cáo không thành công. \nVui lòng thực thực hiện lại.");
                PLException.AddException(e);
            }
            finally {
                if (FrameworkParams.wait != null) FrameworkParams.wait.Finish();
            }
        }