title: exporting datagrid to pdf in Windows Forms
body: <p>Im writing a simple app that allows a user to export the created datagrid (here its dataGridView1) to a pdf file with a little help from itextsharp reference.
  I've found a useful sample code on the internet but im having some problems in configuring it to export the data that the user enters during his work with the app. The user enters the data in textbox, saves it to the datagrid.
  The result pdf file only contains the headers taken from the textbox and no cell content (data entered and saved by users using the textboxes).</p>
  
  <p>Here is the code</p>
  
  <pre><code>        //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.BaseColor(240, 240, 240);
              pdfTable.AddCell(cell);
          }            
  
          //Exporting to PDF
          string folderPath = "C:\\Users\\Marcus\\PDFs\\";
          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();
          }
      }
  </code></pre>