Reoprt form to datagrideview1 data show and this data btnpdfexport click to Export data view pdf formate to datagrideview1 windows form c#
Loading
Reoprt form to datagrideview1 data show and this data btnpdfexport click to Export data view pdf formate to datagrideview1 windows form c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Feb 10, 2024, 5:36 AM EST
Display Data in DataGridView by using the below code sinppet: -
private void Form1_Load(object sender, EventArgs e) { // Fetch and bind your data to the DataGridView DataTable data = GetDataFromDatabase(); // Replace with your actual data retrieval logic dataGridView1.DataSource = data; }Install a PDF library like
iTextSharpto handle the PDF export. You can install it using NuGet Package Manager:Handle the button click event to export data to PDF when the button is clicked.
using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; private void btnPdfExport_Click(object sender, EventArgs e) { ExportDataToPdf(); } private void ExportDataToPdf() { try { // Create a new PDF document using (PdfWriter writer = new PdfWriter("output.pdf")) { using (PdfDocument pdf = new PdfDocument(writer)) { Document document = new Document(pdf); // Add a table to the document iText.Layout.Element.Table table = new iText.Layout.Element.Table(dataGridView1.ColumnCount); // Add header row foreach (DataGridViewColumn column in dataGridView1.Columns) { table.AddCell(new Cell().Add(new Paragraph(column.HeaderText))); } // Add data rows foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { table.AddCell(new Cell().Add(new Paragraph(cell.Value.ToString()))); } } // Add the table to the document document.Add(table); } } MessageBox.Show("Data exported to PDF successfully."); } catch (Exception ex) { MessageBox.Show($"Error exporting data to PDF: {ex.Message}"); } }Amit MohantyRank #112 · 20K reputationPosted Feb 12, 2024, 8:36 AM EST
Please check out my blog below.
https://www.c-sharpcorner.com/blogs/export-datagridview-data-to-pdf-in-c-sharp