Translate

Dienstag, 12. Mai 2015

Visual Basic .net verschiedene Farben pro Zeile in Richtextbox

RichTextBox_Hinweise.SelectionStart = 0
RichTextBox_Hinweise.SelectionLength = 1
RichTextBox_Hinweise.SelectionColor = Color.Red
RichTextBox_Hinweise.SelectionStart =1
RichTextBox_Hinweise.SelectionLength = 1
RichTextBox_Hinweise.SelectionColor = Color.Green

Visual Basic .net Datagridview in PDF exportieren


Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net

25 May 2014  
6 Comments   23301 Views

Here Mudassar Ahmed Khan has explained how to export DataGridView data to PDF file in Windows Forms (WinForms) Applications using iTextSharp PDF conversion library, C# and VB.Net.

DataGridView cannot be exported directly to PDF file and hence need to make use of iTextSharp Table for this purpose.

In this article I will explain how to export DataGridView data to PDF file in Windows Forms (WinForms) Applications using iTextSharp PDF conversion library, C# and VB.Net.
DataGridView cannot be exported directly to PDF file and hence need to make use of iTextSharp Table for this purpose.
 
Form Controls
I have added a DataGridView and a Button to the Windows Form.
Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Reflection;
using iTextSharp.text.pdf;
using iTextSharp.text;
 
VB.Net
Imports System.IO
Imports System.Data
Imports System.Reflection
Imports iTextSharp.text
Imports iTextSharp.text.pdf
 
 
Populating DataGridView
In order to populate the DataGridView, I have created a dynamic DataTable with some sample data.
C#
public Form1()
{
    InitializeComponent();
    this.BindDataGridView();
}
 
private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    this.dataGridView1.DataSource = dt;
}
 
VB.Net
Public Sub New()
    InitializeComponent()
    Me.BindDataGridView()
End Sub
 
Private Sub BindDataGridView()
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), _
                                           New DataColumn("Name", GetType(String)), _
                                           New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
End Sub
 
Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net
 
 
Exporting DataGridView data to PDF
Inside the Button Click event handler, I have written the code for exporting DataGridView data to PDF file.
An iTextSharp PDF Table is created with columns same as that of the DataGridView and then a loop is executed over the DataGridView columns to add their header texts to the PDF Table header row.
Once the header row is populated then loop is executed over the DataGridView rows to create the PDF Table rows.
Then a directory (folder) is created if it does not exists. This folder will be used to save the generated PDF file.
Finally the PDF Table is added to the iTextSharp PDF document and then the PDF document is saved as PDF file to the directory that we had created earlier.
C#
private void btnExportPdf_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);
    }
 
    //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:\\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();
    }
}
 
VB.Net
Private Sub btnExportPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnExportPDF.Click
    'Creating iTextSharp Table from the DataTable data
    Dim pdfTable As New PdfPTable(dataGridView1.ColumnCount)
    pdfTable.DefaultCell.Padding = 3
    pdfTable.WidthPercentage = 30
    pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
    pdfTable.DefaultCell.BorderWidth = 1
 
    'Adding Header row
    For Each column As DataGridViewColumn In dataGridView1.Columns
        Dim cell As New PdfPCell(New Phrase(column.HeaderText))
        cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240)
        pdfTable.AddCell(cell)
    Next
 
    'Adding DataRow
    For Each row As DataGridViewRow In dataGridView1.Rows
        For Each cell As DataGridViewCell In row.Cells
            pdfTable.AddCell(cell.Value.ToString())
        Next
    Next
 
    'Exporting to PDF
    Dim folderPath As String = "C:\PDFs\"
    If Not Directory.Exists(folderPath) Then
        Directory.CreateDirectory(folderPath)
    End If
    Using stream As New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
        Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
        PdfWriter.GetInstance(pdfDoc, stream)
        pdfDoc.Open()
        pdfDoc.Add(pdfTable)
        pdfDoc.Close()
        stream.Close()
    End Using
End Sub
 
Export Windows Forms DataGridView to PDF using iTextSharp, C# and VB.Net
 
 
Downloads
Related Articles
Comments
kennethJun 04, 2014 06:06 AM  121.54.58.247  
I will try this one if it is really working.. Thanks for Sharing..
SACHIN JAINAug 06, 2014 09:08 AM  180.149.39.40  
NICE WORK its working for me. Thanks
NandeshwarFeb 10, 2015 12:02 PM  122.172.166.118  
Finally its working.. Its very useful for me.. Thanks a lot
GouthamApr 08, 2015 09:04 AM  106.51.141.139  
thanks alottt....
sreenivasApr 17, 2015 12:04 PM  198.133.214.10  
Thanks Its working
Add Comments
You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.


Captcha