0

I have a datatable as

  DataTable dt = new DataTable( "Table1" );
  dt.Columns.Add( "c1" );
  dt.Columns.Add( "c2" );
  dt.Columns.Add( "c3" );
  DataRow dr = dt.NewRow();
  dr["c1"] = "100";
  dr["c2"] = "100";
  dr["c3"] = "100";
  dt.Rows.Add( dr );
  dt.AcceptChanges();
  printListView.DataContext = dt;

I have also a listview for showing the table.

ListView                                    
                  HorizontalAlignment="Stretch" 
                  HorizontalContentAlignment="Stretch" 
                  SelectionMode="Single" 
                  ItemsSource="{Binding}" 
                  Name="printListView" 
                  Margin="10"
                  ListView.View
                        GridView
                              GridViewColumn Header="c1" DisplayMemberBinding="{Binding c1}"/
                              GridViewColumn Header="c2" DisplayMemberBinding="{Binding c2}"/
                              GridViewColumn Header="c3" DisplayMemberBinding="{Binding c3}"/
                        /GridView
                  /ListView.View
            /ListView

How can I print this table?

Thanks

Sauron
  • 16,668
  • 41
  • 122
  • 174
  • See answers here: [Print Contents Of A DataTable](https://stackoverflow.com/questions/15547959/print-contents-of-a-datatable/) – Tawab Wakil Apr 10 '20 at 18:39

2 Answers2

2

One way would be to bind the table to a listview and then make a document out of it and print it.

bitbonk
  • 48,890
  • 37
  • 186
  • 278
0

Here's a one-liner that will dump the data to an XML file on disk:

dt.WriteXml(@"c:\temp\MyDataTable.xml");

(In this example, you may need to create the temp folder manually.) Once the file is created, you can open it in your favorite browser or XML viewer and view it or print it.

Tawab Wakil
  • 1,737
  • 18
  • 33