iTextSharp does not matter DataGridView to MySQL and C # pdf

0

I have a Form that generates a DataGridView in which I show fields of a table.

The problem I have is that you export the pdf to me in the following way:

I do not know what's happening. Here is my code that generates the .pdf:

private void button4_Click(object sender, EventArgs e)
        {
            //Creating iTextSharp Table from the DataTable data
            PdfPTable pdfTable = new PdfPTable(datagridview1.ColumnCount);
            pdfTable.DefaultCell.Padding = 3;
            pdfTable.WidthPercentage = 30;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;

            //Adding Header row
            foreach (DataGridViewColumn column in datagridview1.Columns)
            {
                PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
                cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
                pdfTable.AddCell(cell);
            }

            if (pdfTable == null)
            {
                //Adding DataRow
                foreach (DataGridViewRow row in datagridview1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        pdfTable.AddCell(cell.Value.ToString());
                    }
                }
            }

            //Exporting to PDF
            string folderPath = "C:\Users\maury\Desktop";
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
            {
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                pdfDoc.Add(pdfTable);
                pdfDoc.Close();
                stream.Close();
            }
        }

It should be noted that in this line in particular:

if (pdfTable == null)
            {
                //Adding DataRow
                foreach (DataGridViewRow row in datagridview1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        pdfTable.AddCell(cell.Value.ToString());
                    }
                }
            }

I have added the if, since it was generating an NRE (NullReferenceException) But I have made sure to instantiate all my objects correctly. My datagridview I generate directly in the Form, I do not invoke any object of any other kind.

    
asked by Mareyes 23.04.2018 в 01:47
source

1 answer

2

Try something like this to generate the pdf

    public void GenerarDocumentos(Document document)
    {
        Phrase objP = new Phrase("A", fuente);

        //SE GENERA EL ENCABEZADO DE LA TABLA EN EL PDF
        PdfPTable datatable = new PdfPTable(dgv_listaActividades.ColumnCount);
        for (int i = 0; i < dgv_listaActividades.ColumnCount; i++)
        {
            objP = new Phrase(dgv_listaActividades.Columns[i].HeaderText, fuente);
            datatable.HorizontalAlignment = Element.ALIGN_CENTER;

            datatable.AddCell(objP);
        }

        //SE GENERA EL CUERPO DEL PDF
        for (int i = 0; i < dgv_listaActividades.RowCount - 1; i++)
        {
            for (int j = 0; j < dgv_listaActividades.ColumnCount; j++) 
            {
                objP = new Phrase(dgv_listaActividades[j,i].Value.ToString());
                datatable.AddCell(objP);
            }
            datatable.CompleteRow();
        }
        document.Add(datatable);
     }
    
answered by 03.09.2018 / 23:11
source