1:先把页面DataGrid上的数据导入到一个另外一个空DataGrid中:
for (int i = 0; i < StatisticsDetail.Columns.Count; i++)
{
DataGridTextColumn tmpColumn = new DataGridTextColumn();
DataGridTextColumn column = (DataGridTextColumn)StatisticsDetail.Columns[i];
tmpColumn.Header = column.Header.ToString();
System.Windows.Data.Binding bind;
if (column.Binding != null)
{
bind = new System.Windows.Data.Binding();
bind.Path = column.Binding.Path;
bind.Converter = column.Binding.Converter;
}
else
bind = new System.Windows.Data.Binding();
tmpColumn.Binding = bind;
grid.Columns.Add(tmpColumn);
}
grid.ItemsSource = StatisticsDetail.ItemsSource;
//开始打印
Print(grid, documentName);
2:在采用Silverlight中的Print方法打印
private static void Print(UIElement source, string documentName)
{
var doc = new PrintDocument();
var offsetY = 0d;
var totalHeight = 0d;
var canvas = new Canvas();
canvas.Children.Add(source);
try
{
doc.PrintPage += (s, e) =>
{
e.PageVisual = canvas;
canvas.Margin = new Thickness(50);
if (totalHeight == 0)
{
totalHeight = source.DesiredSize.Height;
}
Canvas.SetTop(source, -offsetY);
offsetY += e.PrintableArea.Height;
//是否多页打印
e.HasMorePages = offsetY <= totalHeight;
};
doc.Print(documentName);
}
catch { }
}