Skip to main content
 首页 » 编程设计

c#之如何以编程方式更改 rdlc 报告中的数据表

2024年02月23日14Terrylee

我有以下问题。我制作了一个为我的一位客户生成 rdlc 报告的应用程序。我使用 TableAdapters 来填充我在这些报告中使用的数据表。问题是我的客户有一个新数据库,他希望能够使用该应用程序从旧数据库和新数据库生成报告。我认为可以更改我的 TableAdapter 使用的连接,以便它们只从我的客户选择的数据库中获取数据(两个数据库具有相同的模式),但我被告知这是不可能的。那么是否可以更改rdlc报告使用的DataTable? 我该如何解决这个问题

请您参考如下方法:

您可以使用以下代码在运行时将任何数据表分配给 rdlc。

        DataTable dtTest =obj.SelectDepartment(1);//Here I am selecting the data from DB 
 
        this.reportViewer1.RefreshReport(); 
        reportViewer1.Visible = true; 
        ReportDataSource rds = new ReportDataSource(); 
        reportViewer1.Reset(); 
        reportViewer1.ProcessingMode = ProcessingMode.Local; 
        LocalReport rep = reportViewer1.LocalReport; 
        rep.Refresh(); 
 
        rep.ReportEmbeddedResource = "Report.rdlc";//Provide full path 
        rds.Name = "DataSet1_tblAdapter";//Provide refrerence to data set which is used to   design the rdlc. (DatasetName_TableAdapterName) 
        rds.Value = dtTest; 
 
        rep.DataSources.Add(rds); 
        this.reportViewer1.RefreshReport();