Printing and making reports in c# winform

30,005

Solution 1

You can use the built in reports to generate nice reports wihtout requiring a database.

Create a class for your data, in my case, I am going to create a person class:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
}
  • Next I am going to add a report using the report wizard (Add New Item -> Reporting -> Report Wizard).

  • For the datasource, I am going to select Object and then point it to my Person class. Datasource from a class

  • Select the columns you want for your details, I am just dragging all of them into the values for simplicity.

Report Wizard Fields

  • Walk through the rest of the wizard just selecting the defaults and you should then see your report.

  • Now you can add a ReportViewer control to a form and set the report to the report you just created. This should create a PersonBindingSource on your form as well.

  • Set the PersonBindingSource's data to a list in memory:

    BindingList<Person> myPeople = new BindingList<Person>();
    myPeople.Add(new Person() { FirstName = "John" , LastName = "Doe"});
    myPeople.Add(new Person() { FirstName = "Jane" , LastName = "Doe"});
    myPeople.Add(new Person() { FirstName = "Jerry" , LastName = "Smithers" });
    
    PersonBindingSource.DataSource = myPeople;
    reportViewer1.RefreshReport();
    this.reportViewer1.RefreshReport();
    

With the final report looking like this:

Final Report

Solution 2

Crystal Reports will work perfectly for you. Actually you can generate reports without a database. Check out this project and it should get you started and is just like what you are trying to do.

You this helps you!

Share:
30,005
Andres
Author by

Andres

Updated on July 05, 2022

Comments

  • Andres
    Andres over 1 year

    I used in Delphi QuickReport to create reports and print. What can I use to do this in .NET C#?

    I added some reporting elements (Microsoft reports and Crystal reports) to my project (Winforms app), but what I saw, is that I can only insert data from a Database. What I want, is to use the values of objects created in runtime. This is because my reports actually consists of receipts and invoices.

    Which is the best tool to use for my need?

  • Andres
    Andres almost 11 years
    Thank you really much for your answer. Its really complete and helpful. I wanted to tell you that I do have sap crystal reports installed. You think it is better to use the built in reports instead of crystal ones?
  • John Koerner
    John Koerner almost 11 years
    @Andres There is no right or wrong answer and there is no "better". You need to look at the capabilities of each and determine what works best for your application and use case. Both CR and the microsoft tools have been around a long time and carry with them a very full set of features and some idiosyncratic behavior that is left over from a bygone era. My suggestion is that you do your own research and come to your own conclusions.
  • Andres
    Andres almost 11 years
    Sure! Anyways, I will need to do reports with charts and that kind of stuff later. Does the Microsoft reports support them? Thanks again!
  • Andres
    Andres almost 11 years
    Thank you! Thats a helpful example!
  • panda.o24
    panda.o24 over 8 years
    Thank you for this. I just want to add that the class being referred to as a dataset needs to have a method that returns IEnumerable<T>. I had a hard time figuring it out. I am a complete noob.
  • Drew Chapin
    Drew Chapin over 7 years
    I'm lost at setting the Person class as the data source. The Person class does not show up an option.
  • Drew Chapin
    Drew Chapin over 7 years
    Never mind. Apparently, you have to build the project before doing the Report Wizard.