4

I'm trying to create a report in my WPF application. I'm using Visual Studio 2010, C# and MVVM

I have researched alot and only found articles on the net that deal with Crystal Report by using a database. In my application, I don't communicate with the database directly. We use a web service. It communicates with the database and writes all the data in xml files. We get our info via stored procedures. Hopefully that is not a problem.

Anyway, on one of my User Controls, I have a datagrid that is populated with data from the database. I store this data using a List and Observable Collection. So retreiving the data is easy. But I would now like to export the data into a report for the user to print.

As I said, I've read up on reports, and they all use some form of dataset or datatable. But as I've stated, we don't use the database directly.

I have downloaded the SAP Crystal Reports, developer version for Microsoft Visual Studio: Updates & Runtime Downloads just to ensure I can use Crystal Reports.

I have an idea that I must create a new User Control or Window View and set the datacontext via ViewModel. On this form should be a Report Viewer control, that would be populated. But I don't know what to pass through, if I can do something like this. Can I send over the List/ObservableCollection that has the data I want saved to the form, and bind it to the report control? Using [this] as an example 2, this is kind of the direction I'm looking for

Or is there another way of creating a report without using a database? I just generally want to create a report in my existing WPF project. A new window or user control is opened and the report viewer is loaded here, with the data I have in a gridview. Using WPF and MVVM, how do I achieve this.

I also have Component One if anybody knows how to use this. The website itself doesn't offer great tutorials on the subject of thier report views.

Please let me know if there is anything else you need from me, and I'll make edits where I can

Community
  • 1
  • 1
Nicholas Aysen
  • 568
  • 3
  • 18
  • 40

2 Answers2

1

I think you're on the right track by creating a user control that uses the data grid. I think the next thing you want to look into for printing some kind of reports is DocumentPaginator. Using this approach you can print to .XPS files, print the user control right to paper, or/and use a third-party XPS to PDF converter.

Here is an example to get you started!

Kcvin
  • 5,073
  • 2
  • 32
  • 54
  • seems like something i could use. gonna try format it to look like a form – Nicholas Aysen Mar 04 '14 at 12:54
  • it can be pretty tricky figuring it out, especially when you start going into multiple pages. Basically, when you print using DocumentPaginator, you're telling the printer exactly what to print for each and every page. With that being said you may have to calculate number of rows on a page and what not. I used a PDF virtual printer to print my test runs to a .pdf so I didn't waste paper. – Kcvin Mar 04 '14 at 13:19
  • do you perhaps know of a print preview code snippet? i've seen one around, but i can't find it. or what code do you use for the pdf virtual viewer? – Nicholas Aysen Mar 05 '14 at 06:04
  • I use doPDF v7 as the virtual printer. You just select it in your print dialog when printing and it will print to a file. For the print preview stuff, I implemented my own that shows one page (user control) at a time. You might be able to use [this](http://stackoverflow.com/questions/584551/how-do-i-do-print-preview-when-using-a-documentpaginator-to-print) to help but my version differs from it. – Kcvin Mar 05 '14 at 15:05
1

Just started looking at this myself, firstly you can add a DataSource based on either your EF context or your web service. You can then use the built in reporting toolset (which is based on the SQL Server Report Services (though you dont need this to host them)).

Though you'll need to use the Windows Forms host control :( MSDN walkthrough about hosting reports in WPF

cjb110
  • 1,310
  • 14
  • 30