Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate product receipt in winform application?

I am working on a winform application for the first time and I have a gridview which contains a list of products users have bought.

I have a Print button on click which allows the user to generate a receipt like the one below:

enter image description here

So here I am confused whether I should use "winform default RDLC or Crystal Report" or whether I should generate PDF and then let it print out as receipt, but I am not sure if PDF is a good option for receipt generation or not.

For Crystal Report, I have read that I need to install it and client (who will use this desktop application) had to install Crystal Report and also there is some licensing involve with Crystal Report which I don't want.

Also if I use Crystal Report then I am not sure if it would be possible to generate exactly above receipt (with table formatting) and will it be complicated?

Receipt is bit complicated so is there a better tool or way, or how should I generate receipt I have shown in above image?

Update : Printing paper total size is : 7.50 centimeter and user wants to print all the content in center.

Discount = FinalAmount - MRP;

Customer Name, Mobile No, Bill No, Payment Mode values are entered on the form by user itself.

I am having a Excel file which contains list of products and with each products I have information like ProductId,ProductName,MRP,Tax information like CGST,SGST.

Code to fill gridview from excel file based on Product Id:

 using (OleDbConnection cnnxls = new OleDbConnection(strConn))
                    using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
                    {
                        oda.Fill(dtProductList);
                        DataColumnCollection columns = dtProductList.Columns;
                        if (!columns.Contains("FinalAmount"))
                        {
                            dtProductList.Columns.Add(new DataColumn() { ColumnName = "FinalAmount", DataType = typeof(decimal) });
                        }

                        if (!columns.Contains("Quantity"))
                        {
                            dtProductList.Columns.Add(new DataColumn() { ColumnName = "Quantity", DataType = typeof(int) });
                        }
                        DataRow lastRow = dtProductList.Rows[dtProductList.Rows.Count - 1];
                        lastRow["FinalAmount"] = Convert.ToDecimal(lastRow["MRP"]);
                        lastRow["Quantity"] = 1;
                    }

enter image description here

like image 476
Learning-Overthinker-Confused Avatar asked Mar 04 '23 06:03

Learning-Overthinker-Confused


2 Answers

Generate and print the receipts

You can use any report designer tool like RDLC Reports or Crystal Reports to generate a report. RDLC reports are good enough. You can print the RDLC report with or without showing the print dialog. You can also easily export the RDLC report manually or using the code.

If for any reason you don't want to use a reporting tool, as another option you can consider generating HTML report easily using Run-time T4 templates.

Using an RDLC report, how to show multiple fields in a single cell

You can easily use an expression to show multiple values in a single cell. Also as another option, you can use rows in a single row group and show different fields in a single column.

Example 1 - RDLC - Show multiple fields in a single column using expression

The following steps show you how you can display multiple fields in a single column using expression. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:

  1. Drop a Table from toolbox on the report design surface.
  2. In first column, first data row (not the header row), right click and choose ProductName (image)
  3. Select the header of the second column and type UnitPrice/Quantity (image)
  4. In second column, first data row, right click and choose Expression. (image)
  5. In the expression window, enter the desired expression, for example:

    = "UnitPrice: " & Fields!UnitPrice.Value.ToString() & System.Environment.NewLine & "Quantitye: " & Fields!Quantity.Value.ToString()

Example 2 - RDLC - Show multiple fields in a single column using row group

The following steps show you how you can display multiple fields in a single column. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:

  1. Drop a Table from toolbox on the report design surface.
  2. In first column, first data row (not the header row), right click and choose ProductName (image)
  3. Select the header of the second column and type UnitPrice/Quantity (image)
  4. Right click on row header of the first data row and choose Insert RowInside Group - Below (image)
  5. In second column, first data row, right click and choose UnitPrice. (image)
  6. Click on the [UnitPrice], and then press Home and type UnitPrice: (image)
  7. Do the same for Quantity, in the next row in the group.
  8. If you need another row in the group, repeat step 3. You can setup borders of the cells by selecting them and setting BorderStyle individually for top, left, bottom and right.

Download

You can clone or download an example using expression here:

  • repository
  • zip file
like image 96
Reza Aghaei Avatar answered Mar 09 '23 00:03

Reza Aghaei


A quick and easy way I used before was to generate a html page, and then use the html2pdf library to convert it to a pdf file.

You may also consider this approach since the RDLC reports/Crystal reports may be a overkill for your case.

like image 20
Hainan Zhao Avatar answered Mar 08 '23 23:03

Hainan Zhao