Search code examples
c#winformsdatagridviewdatatablepdfsharp

How to bind datatable to pdfsharp?


I have a data table like this:

// create dataset
DataSet ds = new DataSet("myDataset");

// create datatable
DataTable dt = new DataTable("myDatatable");

// add columns
dt.Columns.Add("column1", typeof(string));
dt.Columns.Add("column2", typeof(string));
dt.Columns.Add("column3", typeof(string));
dt.Columns.Add("column4", typeof(string));

// insert data rows
dt.Rows.Add("row1-col1","row1-col2", "row1-col3", "row1-col4");
dt.Rows.Add("row2-col1","row2-col2", "row2-col3", "row2-col4");

// add datatable to dataset 
ds.Tables.Add(dt);

// bind dataset & datatable to datagridview
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "myDatatable";

I want create pdf reports using pdfsharp. How to bind datatable to pdfsharp?


Solution

  • You cannot bind DataTables to PDFsharp.

    You can use MigraDoc Foundation (comes with PDFsharp) to create a new document, add a table to that document (with the columns you need) and then add the data to that table.

    MigraDoc sample with table:
    http://www.pdfsharp.net/wiki/Invoice-sample.ashx