Jaz KT

Jaz KT

  • NA
  • 2
  • 901

Export To PDF ,The filtered rows of DATA GRID VIEW

Sep 27 2018 5:10 AM
I have a datagrid view,now my intention is to make it to PDF export for the filtered column 
 
My code is below:
 
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1250,BaseFont.EMBEDDED);
PdfPTable table = new PdfPTable(dgv.Columns.Count);
table.DefaultCell.Padding = 3;
table.WidthPercentage = 100;
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.DefaultCell.BorderWidth = 1;
iTextSharp.text.Font txt=new iTextSharp.text.Font(bf,10f, iTextSharp.text.Font.NORMAL);
//ADD HEADER
foreach (DataGridViewColumn colmn in dgv.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(colmn.HeaderText,txt));
cell.BackgroundColor = new iTextSharp.text.BaseColor(245, 245, 220);
table.AddCell(cell);
}
//ADD DATAGRID
foreach (DataGridViewRow row in dgv.Rows)
{
foreach (DataGridViewCell dcell in row.Cells)
{
table.AddCell(new Phrase(dcell.Value.ToString(), txt));
}
}
var SaveFileDialog = new SaveFileDialog();
SaveFileDialog.FileName = filename;
SaveFileDialog.DefaultExt = ".pdf ";
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
using (FileStream stream = new FileStream(SaveFileDialog.FileName, FileMode.Create))
{
Document doc = new Document(PageSize.A4, 10f, 10f, 42f, 35f);
PdfWriter wri = PdfWriter.GetInstance(doc, stream);
doc.Open();
doc.Add(table);
doc.Close();
stream.Close();
}
}
 

Answers (1)