In previous article as we discussed about the adding Crystal report in WPF application and complete the process till creation of all the Columns, now this is the continues part and now we start from here:
- This time also you have to once more Add a New Item to your project and that will be the Crystal Report which is in Reporting template, select it and click Add.
- "Crystal Report Gallery" will be there now, Select "As A Blank Report" option and click the OK button.
- You see, the report will appear in your design mode. Right click on the Database Fields node in the Field Explorer and select "Database Expert".
- Expand the "Create New Connection node".
- Again, Expand "ADO.NET (XML)" node which will display a dialog box in which we have to select the "DataSet1.xsd" file.
- Now, Click on the browse button of "File Path" and select the DataSet1.xsd file to open. And after select the file you will be able to see the full path of your file in the File Path. Then, Click on the Finish button.
- The DataTable1 node will appear now. Click on the > button to move this node to the Selected Tables list and click OK.
- The DataTable1 node will appear under the "Database Fields" node in the Field Explorer window. Expand it to see.
- Drag each field to the details section in the Crystal Report file. And Save it.
- Add the following two assemblies/ dll files to your project:
- SAPBusinessObjects.WPF.Viewer.dll
- SAPBusinessObjects.WPF.ViewerShared.dll
- Also add these highlighted lines to your code:
MainWindow.xaml:
<Window x:Class="WpfCrystalReport.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=
SAPBusinessObjects.WPF.Viewer"
Title="MainWindow" Height="350" Width="525">
<Grid>
<my:CrystalReportsViewer HorizontalAlignment="Left" Name="crystalReportsViewer1"
VerticalAlignment="Top" Height="500" Width="500" />
</Grid>
</Window>
- Use the following two Statements in your MainWindow.xaml.cs:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
And add the following code:
namespace WpfCrystalReport
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ReportDocument report = new ReportDocument();
report.Load("../../CrystalReport1.rpt");
using (WpfApplication4Entities1 db = new WpfApplication4Entities1())
{
report.SetDataSource(from c in db.Clients
select new { c.CustomerName, c.Address, c.EmailId, c.PassportNumber});
}
crystalReportsViewer1.ViewerCore.ReportSource = report;
}
}
}
- Lastly hit F5 to run your application and the result will be:
Happy Learning...