Reporting + MVVM + WPF + DataSource

2 posts, 0 answers
  1. Thorsten
    Thorsten avatar
    3 posts
    Member since:
    Mar 2013

    Posted 24 Sep Link to this post

    Hi,

    I have a ReportViewer control:

    <tr:ReportViewer ReportSource="{Binding MyReportSource}"/>


    A report:

    public partial class MyReport : Telerik.Reporting.Report
    {
        public MyReport()
        {
            InitializeComponent();
        }
    }

     

    A view model:

    public class ViewModel
    {
        public ReportSource MyReportSource { get; set; } = new InstanceReportSource { ReportDocument = new MyReport() };
        public IList<MyReportModel> MyModels { get; set; }
    }

     

    The ReportViewer control displays the structure defined in MyReport.

    I assume that the ReportSource binding is working properly.

     

    But how do i bind MyModels as a data source for MyReportSource,

    so that it actually renders some data?

     

    Thanks in advance,

    machine spirit

     

  2. Thorsten
    Thorsten avatar
    3 posts
    Member since:
    Mar 2013

    Posted 26 Sep in reply to Thorsten Link to this post

    I somehow figured (fiddled) it out...

     

    The Report (MyReport.Designer.cs), had this defined:

    private Telerik.Reporting.ObjectDataSource DataSource;

     

    Seems to have been added by the designer.

    Also it hides the 

    Telerik.Reporting.Report.DataSource

    property.

     

    So i added one line to the Report constructor:

    public partial class MyReport : Telerik.Reporting.Report
    {
        public ReservationDetailsReport()
        {
            InitializeComponent();
            DataSource.DataSource = MyReportParameter.MyModels;
        }
    }

     

    And a new static class to act as a bucket for MyModels:

    public static class MyReportParameter
    {
        public static IList<MyModel> MyModels { get; set; }
    }

     

    In my ViewModel i had to do exatly the following:

    public class ViewModel
    {
        public IList<MyReportModel> MyModels { get; set; }
         
        private readonly MyReport myReport = new MyReport();
     
        public void UpdateReport()
        {
            MyReportSource = null;
             
            MyReportParameter.MyModels = MyModels;
     
            MyReportSource = new TypeReportSource
            {
                TypeName = myReport.GetType().AssemblyQualifiedName
            };
        }
    }

     

    For me none of this os obvious.

    I have not found any hints to this in the documentation either.

    Maybe this will help somebody.

     

    Cheers!

Back to Top