Introduction
This is an example of how to print an Invoice with C#. It shows.
- How to create a report using PrintPreviewDialog control and PrintDocument control?
- How to draw an Invoice head?
- How to draw the table of products and their prices?
- How to compute and draw an Invoice total?
![preview]()
My Project has three forms.
- frm Invoice: to bind DataGrid with all Orders from the Northwind database file.
- frm Input: Choose one Order for which you want to print its Invoice.
- frm Order: to display the Invoice on DataGrid, then you can Print a Preview or Print the Invoice as a Report.
Also, we need three classes for printing.
- System.Windows.Forms.PrintDialog.
- System.Windows.Forms.PrintPreviewDialog.
- System.Drawing.Printing.PrintDocument.
Of course, you can use any database file instead of Northwind.mdb and change my code to connect with the database; also, you can change my SQL string to bind a DataGrid with data.
Example. Bind the DataGrid in from invoice form with all Orders.
Example. Bind the DataGrid in the frmOrder form with one Order.
Declare and initialize three instances for printing
In C#, you can declare and initialize instances of classes or objects to perform various tasks, including printing information to the console. In this article, we'll demonstrate how to declare and initialize three instances for printing.
Here is the code below that initializes three instances for printing.
To draw something on the report (as a line or text)
When creating reports, it's often necessary to add various elements like lines, text, shapes, or images to convey information effectively. Most reporting tools and frameworks provide features for drawing and adding elements to your reports. In this guide, we'll explore how to draw something on a report, be it a line or text, using a hypothetical reporting tool called "ReportBuilder."
Here is the code below To draw something on the report (as a line or text).
- Get Left Margin, Right Margin, Top Margin, Bottom Margin, Report Width and Report Height.
- Set Font and Color.
- Set the Font Height and Font Width and coordinate, then use the DrawString method.
The project has several pieces of code in three forms; please read the code, then run the program to see the result.