8

I have written an application in c# and now i want to print its content in form of invoice as shown in figure i want to print costumer data only once but jobs he has asked to be performed on his car shown in datagrid view should be there in form of list with labour and total labour at the end of invoice. some people suggested to use crystal reports I have never used them so looking for a simpler solution cutting it short how can we print required values from formalt text

Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
  • Something like Crystal Reports is probably your best bet. There are other reporting tools out there. Telerik have some (but they're pricey). – ChrisF Dec 06 '10 at 20:49
  • but vs 2010 do not have crystal reports and when it comes to publish this software on client machine where there is no vs 2010 then installing crystal reports may be required so that is a bit ...you know – Afnan Bashir Dec 06 '10 at 20:53
  • 1
    i use http://www.devexpress.com/Products/NET/Printing/index.xml?tab=features, pretty simple, however, $150 – Luke Hutton Dec 06 '10 at 21:04
  • 1
    Google 'print datagridview', take the first hit. – Hans Passant Dec 06 '10 at 21:13

3 Answers3

7

The easiest and quickest solution is to use the Visual Basic PowerPack's PrintForm control (You can use it in C# projects as well).

http://msdn.microsoft.com/en-us/vbasic/bb735936.aspx

Just drag the control on to your form then from code call

printForm1.Print();

This will print whatever is on the form, so just design your report on a form then call that code, and you're done.

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
  • what about redistribution of my application? dose it require to beinstalled – Afnan Bashir Dec 07 '10 at 05:05
  • You can either deploy the dlls with your application, or you can use the included bootstrapper package to easily redistribute them in a clickonce or setup project. It's very small and simple, see the link in my answer for more information. – theChrisKent Dec 07 '10 at 13:47
  • This will print directly without showing a preview, how can I add a preview control for this?!! – Mȍhǟmmǟd Șamȋm Nov 01 '20 at 12:03
  • 1
    Found the answer putting it here for others In the Properties window of **printForm1**, set the **PrintAction** property to **PrintToPreview**. – Mȍhǟmmǟd Șamȋm Nov 01 '20 at 12:16
3

The last time I needed to print a few fields from a C# form, I simply created a Bitmap image using the "Bitmap" and "Graphics" object, and used "PrintDocument" to print it.

The layout of the printed report is done in code by specifying coordinates of the elements to be printed. It's cheap and dirty, but works.

Moshe
  • 2,638
  • 2
  • 28
  • 32
3

You can use a local SSRS report (.rdlc extension). There should be a new item template under "Reporting" for creating the report. The report is displayed in Winforms using the Report viewer control (the control can also be used in a WPF application using a WindowsFormsHost). The only downside is a required dependency that must be installed with your application. Here is the redistributable package for the 2010 report viewer.

A plus is the report can easily be hosted in an SSRS instance if/when it will need to be viewed from a browser. The viewer can also render reports locally that are hosted in an SSRS instance.

There are plenty of guides online for using the report viewer depending on the data source to use for populating the report. The following example is using a generic list as the datasource. The "labels" in the new ReportDataSource line has to be the same as the name of the Dataset in the report definition. The properties of the generic object must also match the column names of the Dataset.

    public ReportViewer(IEnumerable<UnprocessedLabel> labels)
    {
        InitializeComponent();

        var reportViewer = new Microsoft.Reporting.WinForms.ReportViewer { ProcessingMode = ProcessingMode.Local };
        reportViewer.LocalReport.ReportPath = System.IO.Path.GetDirectoryName(Application.ResourceAssembly.Location) + "\\UnprocessedPalletLabel.rdlc";
        var ds = new ReportDataSource("labels", labels);
        reportViewer.LocalReport.DataSources.Add(ds);
        reportViewer.RefreshReport();
    }
miyamotogL
  • 513
  • 3
  • 10