PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

How to use a forEach loop populating data into table cells
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4137
Page 1 of 1

Author:  newbieCoder34 [ Wed May 27, 2020 5:19 pm ]
Post subject:  How to use a forEach loop populating data into table cells

I have passed in some data into the DefineInvoice function from the database we are using using this code:

Code:
public void DefineInvoice(Document document,List<TicketView1> SampleTickets)


Looking into the data of SampleTickets we have key/value pairs for example such as this:

agreement_name: "Website Development 101"
billable_hrs: 9
customer_id: 36
customer_name: "Python Bank"
date_created: {4/29/2020 12:00:00 AM}...

and so forth. I have a table already created like so:

Code:
//define table
            Table table = section.AddTable();
       table.Borders.Width = 0.75;
           

            Column column = table.AddColumn(Unit.FromCentimeter(7));
            column.Format.Alignment = ParagraphAlignment.Center;

            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;

            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;

            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;

            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;
           

            //define header of table
            Row row = table.AddRow();
            row.HeadingFormat = true;
            Cell cell = row.Cells[0];
            cell.AddParagraph("Customer Name");
            cell.Format.Font.Bold = true;

            cell = row.Cells[1];
            cell.AddParagraph("Date Created");
            cell.Format.Font.Bold = true;

            cell = row.Cells[2];
            cell.AddParagraph("Description");
            cell.Format.Font.Bold = true;

            cell = row.Cells[3];
            cell.AddParagraph("Due Date");
            cell.Format.Font.Bold = true;

            cell = row.Cells[4];
            cell.AddParagraph("Billable Hours");
            cell.Format.Font.Bold = true;

            //define one row of invoice information
            row = table.AddRow();
            cell = row.Cells[0];
            cell.AddParagraph("Admin theme with psd project layouts");
            cell = row.Cells[1];
            cell.AddParagraph("1");
            cell = row.Cells[2];
            cell.AddParagraph("$26.00");
            cell = row.Cells[3];
            cell.AddParagraph("$5.98");
            cell = row.Cells[4];
            cell.AddParagraph("$31.98");

            //define another row of invoice information
            row = table.AddRow();
            cell = row.Cells[0];
            cell.AddParagraph("Wordpress Theme customization");
            cell = row.Cells[1];
            cell.AddParagraph("2");
            cell = row.Cells[2];
            cell.AddParagraph("$80.00");
            cell = row.Cells[3];
            cell.AddParagraph("$36.80");
            cell = row.Cells[4];
            cell.AddParagraph("$196.80");

            //define another row of invoice information
            row = table.AddRow();
            cell = row.Cells[0];
            cell.AddParagraph("Angular 9 and Node JS Application");
            cell = row.Cells[1];
            cell.AddParagraph("3");
            cell = row.Cells[2];
            cell.AddParagraph("$420.00");
            cell = row.Cells[3];
            cell.AddParagraph("$193.20");
            cell = row.Cells[4];
            cell.AddParagraph("$1033.20");

            //add invisible row as a space line to the table
            row = table.AddRow();
            row.Borders.Visible = false;

            //add the subtotal row
            row = table.AddRow();
            row.Cells[0].Borders.Visible = false;
            row.Cells[0].AddParagraph("Sub Total:");
            row.Cells[0].Format.Font.Bold = true;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
            row.Cells[0].MergeRight = 3;

            //add tax row
            row = table.AddRow();
            row.Cells[0].Borders.Visible = false;
            row.Cells[0].AddParagraph("TAX:");
            row.Cells[0].Format.Font.Bold = true;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
            row.Cells[0].MergeRight = 3;

            //add total
            row = table.AddRow();
            row.Cells[0].Borders.Visible = false;
            row.Cells[0].AddParagraph("TOTAL:");
            row.Cells[0].Format.Font.Bold = true;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
            row.Cells[0].MergeRight = 3;


As you can see for my table headers such as "Customer name" I want to put that customer_name value into that cell. How would I do that? So far I have:

Code:
foreach (TicketView1 ticket in SampleTickets)
            {
                //customer_name
                //date_created
                //description
                //due_date
                //billable_hrs
            }


I am also learning C# as I go.. or I at least know most of the basics so this is tough for me. Any hints/tips or suggestions?

Author:  Thomas Hoevel [ Thu May 28, 2020 11:03 am ]
Post subject:  Re: How to use a forEach loop populating data into table cel

In the loop, call "AddRow()" to add a new row and then add the data to the cells in the column where the data should be.

MigraDoc sample with such a loop:
http://www.pdfsharp.net/wiki/Invoice-sa ... _Content_3

Author:  newbieCoder34 [ Sat May 30, 2020 1:52 am ]
Post subject:  Re: How to use a forEach loop populating data into table cel

I didn't see a loop inside the migradoc sample. Using the forEach loop along with what you said helped me solve the problem like this:

Code:
foreach (TicketView1 ticket in SampleTickets)
            {
                row = table.AddRow();

                cell = row.Cells[0];
                cell.AddParagraph(ticket.customer_name);

                cell = row.Cells[1];
                cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));

                cell = row.Cells[2];
                cell.AddParagraph(ticket.description);

                cell = row.Cells[3];
                cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));

                cell = row.Cells[4];
                cell.AddParagraph(ticket.billable_hrs.ToString());
            }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/