how to bind datasource to a .rdlc report in c#

橙三吉。 提交于 2020-01-04 09:54:19

问题


Friends , I have developed a simple application using c# , it has two rdlc reports

i used this below code to bind datasource to report viewer

 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();

But when the report is generated ,it is empty report no data will displayed , any opinion???


回答1:


When you add .rdlc report in your project by wizard then by default it take dataset name as 'DataSet1' . Now if you want to bind dynamically new dataset then name of that dataset must be 'DataSet1'. Try change it and also check that Table[0] contains some data(Rows) for which DataType get matched with original dataType of DataSet1. If DataType doesn't matches then data wont come in your ReportViewer. Try this code:-

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();

For more detail about .rdlc report(Core logic) refer following link How to create report (RDLC) without database?




回答2:


try below, may be the problem with data source name incorrect.

reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(ds.DataSetName + "_" + ds.Tables[0].TableName, ds.Tables[0]));

you can check dataset name on the rdlc file content. check the name property of the dataset match with what you have given in the code.




回答3:


This is how I update my data with object binding: In the Form1.cs file:

private myClass m_products = new Products();
public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           this.PaperBindingSource.DataSource = m_products.GetProducts();

The this.PaperBindingSource.DataSource is important.



来源:https://stackoverflow.com/questions/15894416/how-to-bind-datasource-to-a-rdlc-report-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!