如何将数据源绑定到c#中的.rdlc报告

Ros*_*han 2 c# rdlc dataset winforms

朋友们,我用c#开发了一个简单的应用程序,它有两个rdlc报告

我使用下面的代码将数据源绑定到报表查看器

 this.reportViewer1.LocalReport.ReportPath = @"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\reports\reports\Report1.rdlc";
 reportViewer1.LocalReport.DataSources.Clear();
 reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("customer", dt.Tables[0])) ;
 this.reportViewer1.RefreshReport();
Run Code Online (Sandbox Code Playgroud)

但是当报告生成时,它是空的报告没有数据显示,任何意见???

Ank*_*kar 7

当您通过向导在项目中添加.rdlc报告时,默认情况下它将数据集名称设置为"DataSet1".现在,如果要动态绑定新数据集,则该数据集的名称必须为"DataSet1".尝试更改它,并检查Table [0]是否包含DataType与原始dataType匹配的一些数据(行)DataSet1.如果DataType不匹配,那么数据将不会出现在ReportViewer中.试试这个代码: -

string exeFolder = (Path.GetDirectoryName(Application.StartupPath)).Substring(0, (Path.GetDirectoryName(Application.StartupPath)).Length - 3);
string reportPath = Path.Combine(exeFolder, @"Reports\SessionReport.rdlc");
Microsoft.Reporting.WinForms.ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", yourDataSet.Tables[0]);
this.reportViewer1.LocalReport.DataSources.Add(rds);
this.reportViewer1.LocalReport.ReportPath = reportPath;
this.reportViewer1.RefreshReport();
Run Code Online (Sandbox Code Playgroud)

有关.rdlc报告(核心逻辑)的更多详细信息,请参阅以下链接 如何在没有数据库的情况下创建报告(RDLC)?