Skip to content

Export To PDF ,The filtered rows of DATA GRID VIEW

Jaz KT

Asked Sep 27, 2018, 1:10 AM EDT1 answer
Share
LinkedInX
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);
Java
 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();


 }
 }
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Farhan Ahmed
    0
    https://www.aspsnippets.com/Articles/Export-selected-checked-GridView-Rows-to-PDF-in-ASPNet.aspx
    0