有关以编程方式更改水晶报告的问题

时间:2009-09-25 14:53:07

标签: c# visual-studio-2008 .net-3.5 crystal-reports

一般信息:C#,VS2008,.NET 3.5

我的表格里面有一个水晶报表查看器,我想用一个表格来显示我所有的水晶报表。我已经得到它以便我可以以编程方式更改报告,唯一的问题是我必须为每个水晶报告采用新方法。我想创建一个可以接受数据库名称,服务器名称,然后是CrystalReport的方法。下面显示的代码位于具有水晶报告查看器的表单上。

public void setReport(string databaseName, string serverName, ReportType report)
{
        crystalReportViewer1.ReportSource = report; 

        CrystalDecisions.Shared.ConnectionInfo crDbConnection = new CrystalDecisions.Shared.ConnectionInfo();
        crDbConnection.IntegratedSecurity = false;
        crDbConnection.DatabaseName = databaseName;
        crDbConnection.ServerName = serverName;
        crDbConnection.UserID = "userid";
        crDbConnection.Password = "password";
        CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt = (CrystalDecisions.CrystalReports.Engine.ReportDocument)crystalReportViewer1.ReportSource;
        CrystalDecisions.CrystalReports.Engine.Database crDatabase = oRpt.Database;
        CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo;
        foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in crDatabase.Tables)
        {
            oCrTableLoginInfo = oCrTable.LogOnInfo;
            oCrTableLoginInfo.ConnectionInfo = crDbConnection;
            oCrTable.ApplyLogOnInfo(oCrTableLoginInfo);
        }
    }

2 个答案:

答案 0 :(得分:0)

我用过这个:

 public enum ReportType : int //Must match actual rdlc filename!!
        {
            rptBankPaymentInstructions = 0,
            rptInstructionReconciliation = 1,
            rptMarketValue = 2,
            rptPortfolioInstructionsSummary = 3
        }

private void ShowRpt(ReportType type, string[] parametersValues, string[] parameterNames)
        {
            try
            {
                reportViewer1.LocalReport.DataSources.Clear();
                reportViewer1.LocalReport.ReportEmbeddedResource =  "ATPresentation.Reports." + type.ToString() + ".rdlc";
                //reportViewer1.LocalReport.ReportEmbeddedResource = "ATPresentation.Reports.rptMarketValue.rdlc";
                ReportEngineService.ReportService rService = new ATPresentation.ReportEngineService.ReportService();


                ReportDataSource rds = new ReportDataSource();


                switch (type)
                {
                    case ReportType.rptBankPaymentInstructions:
                        break;
                    case ReportType.rptMarketValue:
                        rds.Name = "Report_MarketValue";

                        ReportEngineService.Report.MarketValueDataTable dt = rService.GetMarketValueDt(Convert.ToInt32(parametersValues[0]), Convert.ToDateTime(parametersValues[1]));
                        rds.Value = dt;
                        //reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("Report_MarketValue",dt));
                        break;
                    default:
                        break;
                }

                List<ReportParameter> allParams = new List<ReportParameter>();

                for (int i = 0; i < parametersValues.Length; i++)
                {
                    Microsoft.Reporting.WinForms.ReportParameter rptParams = new ReportParameter( parameterNames[i], parametersValues[i]);
                    allParams.Add(rptParams);
                }

                reportViewer1.LocalReport.SetParameters(allParams);           
                reportViewer1.LocalReport.DataSources.Add(rds);
                reportViewer1.RefreshReport();
            }
            catch (Exception)
            {

                throw;
            }


        }

答案 1 :(得分:0)

我的问题的解决方案是我只需要将报告作为对象传递,然后将报告源设置为等于该对象。

相关问题