How to pass object attributes to report (RDLC) C#

Viewed 379

I want to fill a report page with attributes of an object and display it in a reportViewer. This object I selected before in the Report Wizard and a DataSet was generated.

Report Designer

Now I am trying to use this code to add the values to the report. I am putting that single object into a list because "ReportDataSource" doesn't accept objects. But when I run it, the report is blank and the values were not displayed. What am I doing wrong? I am new to reports and I hope someone can help me.

    private void button2_Click(object sender, EventArgs e)
    {
        MyObject myobject = new MyObject();
        myobject.Artikelnr = "12345";
        myobject.Aussehen = 1;
        myobject.Bemerkungen = "cool";

        List<MyObject> objectlist = new List<MyObject>();
        objectlist.Add(myobject);

        ReportDataSource rds = new ReportDataSource("DataSet1", objectlist); 
            
        reportViewer1.LocalReport.DataSources.Add(rds);
        reportViewer1.Refresh();
    }
2 Answers

Make sure you either refresh the report with the green refresh arrow on the toolbar and/or delete all the .data files. Visual Studio caches data results and only refreshed when a parameter changes, the dataset is refreshed. You could be running into a cache issue.

This link is helpful, you can download this project and will test it. Before adding a data source, is better to clear it.

 private void button2_Click(object sender, EventArgs e)
    {
        MyObject myobject = new MyObject();
        myobject.Artikelnr = "12345";
        myobject.Aussehen = 1;
        myobject.Bemerkungen = "cool";

        List<MyObject> objectlist = new List<MyObject>();
        objectlist.Add(myobject);

        ReportDataSource rds = new ReportDataSource("DataSet1", objectlist); 

        ReportViewer1.LocalReport.DataSources.Clear();  

        reportViewer1.LocalReport.DataSources.Add(rds);
        reportViewer1.Refresh();
    }
Related