1

Im trying to 'print' the contents of my datagrid / datatable. I read something about converting DATAGRID To TABLE To FLOWDOCUMENT but I can't find a source how to do it.

I just need some sample codes on how to convert datagrid/dataTable to Table .

then I will just apply this solution : Printing a WPF FlowDocument

any idea?

Thanks and Sorry about for my noob question.

Community
  • 1
  • 1
nfinium
  • 141
  • 3
  • 8

1 Answers1

2

As far as my research went when I printed something I found that converting UI elements to pages is a wrong thing to attempt. Printing is a lot different then creating UI and there are no scrolls or draggable columns on paper :) so your data for printing will differ form data displayed on your forms.

As far as DataTable goes this is only an representation of data so you can easily produce a table that can be printed it already has rows and columns so it should be straight forward to begin: (note code not tested)

System.Data.DataTable dataTable = GetDataTable();
var table = new Table();
var rowGroup = new TableRowGroup();
table.RowGroups.Add(rowGroup);
var header = new TableRow();
rowGroup.Rows.Add(header);

foreach (DataColumn column in dataTable.Columns)
{
    var tableColumn = new TableColumn();
    //configure width and such
    table.Columns.Add(tableColumn);
    var cell = new TableCell(new Paragraph(new Run("column Header")));
    header.Cells.Add(cell);
}

foreach (DataRow row in dataTable.Rows)
{
    var tableRow = new TableRow();
    rowGroup.Rows.Add(tableRow);

    foreach (DataColumn column in dataTable.Columns)
    {
        var value = row[column].ToString();//mayby some formatting is in order
        var cell = new TableCell(new Paragraph(new Run(value)));
        tableRow.Cells.Add(cell);
    }
}

//and print your table

If you ask me I find this API a little bit verbose but for complex documents with proper understanding it is very powerful.

Rafal
  • 12,391
  • 32
  • 54