3

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

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • You can use any report designer tool to generate a report like above image. RDLC reports are good enough. You can [print the RDLC report with or without showing the print dialog](https://stackoverflow.com/a/34728429/3110834). You can also easily [export the RDLC report manually or using the code](https://stackoverflow.com/a/40409777/3110834). – Reza Aghaei Apr 03 '19 at 19:22
  • Also as another option you can consider [generating HTML report easily using Run-time T4 templates](https://stackoverflow.com/a/39713817/3110834). – Reza Aghaei Apr 03 '19 at 19:26
  • @RezaAghaei Thank you so much for replying and suggesting options but 1 thing i dont understand is if you can see my table structure I am having MRP,QTY,DISC in 1 column so how do i cater to it? – I Love Stackoverflow Apr 04 '19 at 06:18
  • Right click on row header in the tablix and insert a new row in the same group. This way you can easily bind the new cell to a different data field. You cal also apply formatting to the cell content using expression, to show "label: value" inside a cell. – Reza Aghaei Apr 04 '19 at 08:00
  • @RezaAghaei I will bind datatable with data source of rdlc report so how could i have 3 values(MRP,QTY,DISC) in 1 column of datatable? – I Love Stackoverflow Apr 04 '19 at 12:07
  • 1
    You can use RDLC Report for this.It is possible to print PDF,WORD,EXCEL. – Md Abdul Mannan Apr 01 '19 at 11:36
  • I want to generate this receipt directly not further export in PDF/Word/Excel – I Love Stackoverflow Apr 01 '19 at 11:45
  • @RezaAghaei I have updated question with more details – I Love Stackoverflow Apr 05 '19 at 07:07
  • @Learning-Overthinker-Confused In two ways: first, the way that I suggested in the previous comment by adding two new rows to the same row group. The other is using a formula column/expression. – Reza Aghaei Apr 05 '19 at 08:16
  • @RezaAghaei Which would be more better : RDLC or Runtime T4 templates? – I Love Stackoverflow Apr 05 '19 at 10:33

4 Answers4

4

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:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Upvoted for writing steps in details but for 1st column i.e Particulars/HSN where I want to display value of 3 fields i.e ProductId,ProductName,HSN So I declare all this 3 properties(ProductId,ProductName,HSN) in Datatable of my Dataset? – I Love Stackoverflow Apr 06 '19 at 10:10
  • The same steps can be done for the first column, using expression or using row group. – Reza Aghaei Apr 06 '19 at 11:06
  • I have send you table generated in RDLC report with following your steps in chat because i cannot send you screenshot in comment so – I Love Stackoverflow Apr 06 '19 at 11:30
  • 1
    @Learning-Overthinker-Confused I created an example in github. You can clone/download it to have a better idea of the solution. – Reza Aghaei Apr 06 '19 at 13:37
  • Yes I have downloaded it and its working fine with expressions but border is not coming as you can see here : https://i.stack.imgur.com/poFGb.png – I Love Stackoverflow Apr 06 '19 at 14:01
  • The border in the image looks fine. I'me not sure what you mean by border is not coming. But you can play with `BorderStyle` property of the cells. Also the other solution (using row groups) is working. I think now you have all you need and you can play with the cell properties to make it to fit to your requirements :) – Reza Aghaei Apr 06 '19 at 14:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191365/discussion-between-learning-overthinker-confused-and-reza-aghaei). – I Love Stackoverflow Apr 06 '19 at 14:08
  • Thank you so much for all your time,guidance and help.It really helped me and so far I have been successfull in generating receipt like above.That expr part from your code repository really helped me.Really really appreciated.Can I ask you 1 last question please? – I Love Stackoverflow Apr 10 '19 at 07:24
  • You're welcome :) feel free to ask the question, I'll help if I have an answer. – Reza Aghaei Apr 10 '19 at 08:23
  • I have posted question : https://stackoverflow.com/questions/55633350/how-to-show-report-data-in-center-of-rdlc-report – I Love Stackoverflow Apr 11 '19 at 13:14
1

The RDLC is powerful as well as Crystal reports. You may choose the rdlc which comes close in eliminating licensing costs.

Using RDLC

Data You need to add datasets Here or data sources to the report which you will manipulate to meet the design and data you want.

Design On design you just drag and drop controls to your taste. There is a challenge that sometimes what you see on the design may not what be you see on final output so you need to test much.

Printing You can put a print preview or send directly to a pdf viewer using rdlc. Here is an example.

Conclusion I think If you have your data generated well on the report, the design and layout won't be much of a problem using both rdlc and crystal reports.

UPDATE Based on further information provided I have tried to do something that may come close to what you want to achieve. I have used crystal reports as well as database table to simulate because of time. Otherwise the same can be achieved using rdlc.

The sample table i created enter image description here

Here is the sample query and results from the database. I have made groups that can be accomodated by the crystal reports. You can do calculated text values using the same to put distinction between the Tax information as well as Transaction Memo. enter image description here

Here is the final look after tweaking the design. The page layout may also be tweaked with regards to your taste.

Update. For RDLC I think you need to add datasets for memo data and tax information. Take a look at the below if it comes close. I failed to make a preview there were components I hadn't installed. enter image description here

enter image description here

Gnyasha
  • 658
  • 11
  • 19
  • But How do i display MRP,QTY,DISC in 1 column as shown in my receipt? – I Love Stackoverflow Apr 04 '19 at 06:19
  • If they are coming from a database its easy. You may need to CONCAT MRP,QTY,DISC as a single string so that each row comes with the 3 (MRP,QTY,DISC). I would try if there is sample data. For example on the memo table on the report I think it is possible to run a database query that produces the first 3x3 columns. – Gnyasha Apr 04 '19 at 15:12
  • Kindly check the answer If the report comes close to what you are expecting. I tried using crystal reports and fetching the data from a table although some of the data may not be properly calculated per your needs. – Gnyasha Apr 05 '19 at 16:37
  • I appreciate all the work that you have done for me but I am not using crystal report.I am currently trying to make this work with RDLC because crystal report requires license – I Love Stackoverflow Apr 06 '19 at 06:51
  • Could you please help me achieve this using RDLC? – I Love Stackoverflow Apr 06 '19 at 13:18
  • Hi I have added an image for rdlc. I failed to add components to preview the report but I think it may help. – Gnyasha Apr 09 '19 at 06:21
  • Upvoted for your kind efforts towards helping me and for your valuable time.Really really appreciated.thanks alot :) – I Love Stackoverflow Apr 10 '19 at 07:25
1

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.

Hainan Zhao
  • 1,962
  • 19
  • 19
  • *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](https://stackoverflow.com/a/39713817/3110834).* – Reza Aghaei Apr 09 '19 at 03:07
  • Upvoted for your kind effforts towards hellping me and for your valuable time.Appreciated :) – I Love Stackoverflow Apr 10 '19 at 09:44
1

for adding 3 columns in one cell you have two options:

1- Use new line expression

=Fields!MyField1.Value + System.Environment.NewLine + Fields!MyField2.Value

2- Use something like subreport or grouping in rdlc.

the first option seams easier

Mustafa Muawia
  • 120
  • 1
  • 4