Wednesday, September 25, 2013

Implementing SSRS Reports in WPF Application - Model -View - ViewModel...


Implementing SSRS Reports in WPF Application - NOT Model -View - ViewModel...
 
Purpose of M-V-VM:
M-V-VM is called as Model - View- ViewModel Pattern which is mainly used for WPF - Windows Presentation Foundation as well as Silverlight Application. Windows Application are strongly coupled with Code Behind file where all the Event of the Controls are written. In case of WPF, the XAML File has Code behind file but the Event fires are written separately in the ViewModel files. these kind of Architecture are called as M-V-VM and they are loosely coupled or Decoupled. Advantage of M-V-VM are the Event Code and UI are not dependent on each other. Unit testing can easily performed on these ViewModel Code
 
Step 1: Create a View, View Model and Model Folder in the Solution as shown below


Step 2: Create a View using the XAML page or using User Control
Step 3: Update the Design Height Properties = 640 and Design Width Properties = 1330. Add the Reference DLL file from Assemblies -- > Extension -- >  Microsoft.ReportViewer.WinForms
Step 4: Add the Reference in the XAML file as shown below

Step 5: Add the Control called  Content Presenter with the respective Binding properties along with the Height and Width Properties. Binding Properties contains a Viewer which is the set as a Properties in View Model which will be shown in few screen down.
Step 6: Create ViewModel Class as SalesReportViewModel.cs and declare the class as public and create the public constructor as shown. Also add the Namespace of System.Window.Forms.Integration and Microsoft.ReportViewer.WinForms. Window.Forms.Integration is present in the Path C:\Program Files\Reference Assemblies\Microsoft\Framework\V3.0
Step 7: Add the below code in the Constructor. ReportViewer is a Object Instance created dynamically and gets loaded into a Class called Viewer of Type WindowFormHost. That why we added Window.Forms.Integration DLL into the Solution.

ReportViewer = new ReportViewer();
ReportViewer.ProcessingMode = ProcessingMode.Remote;
String K = "http://ReportServer/SQL2012";
ReportViewer.ServerReport.ReportServerUrl = new Uri(K);
ReportViewer.ServerReport.ReportPath = "/Reports/SalesReport";
ReportViewer.RefreshReport();
ReportViewer.Show();
this.Viewer.Child =  ReportViewer;

Step 8: Add the Viewer Properties of Class type WindowsFormHost  as shown below 

public ReportViewer ReportViewer;
private WindowsFormsHost _viewer = new WindowsFormsHost();
public WindowsFormsHost Viewer
{
       get
       {
                return _viewer;
       }           
      set
     {
           _viewer = value;
                NotifyPropertyChanged("Viewer");
     }
} 
 
private void NotifyPropertyChanged(string p)
{
     throw new NotImplementedException();
}

Step 9: Add the code shown below in the screenshot inside the Main Window and set the Properties like Window Style = Single Border Window, Window State = Maximized,  Min Width and  Min Height. Once done build the Solution and run the application. it should load the WPF Window with Menu.
 On Running the application
 
Step 10: Now write the ICommand and RelayCommand in ViewModel of Sales Report.
 
Code and Screenshot will be uploaded soon. Kindly wait for a Day.
 
Happy Coding :-)
 
Regards,
Daniel J,
Dataware and Data Mining Architect...
 

1 comment:

Unknown said...

Thank you Daniel. Your article was really valuable for me. Keep up the good work.!!!