Printing a datagrid

Mar 27, 2009 at 10:24 AM
I'm trying to print a datagrid in WPF and I may be barking up the wrong tree.

I use the following code, some of which is ruthlesslesly clipboarded off the intarwebs.

There are several problems. I use a dedicated, in-memory, control and copy the ItemsSource to it and measure/arrange it (in memory) to its preferred size. This takes a lot of time and eats a *lot* of memory which is never freed again. I then capture it screen by screen and print it. Only the first page is actually printed although it seems it got the number of pages right as it includes them they're just empty. I tried using a FixedDocument with the same pattern with almost the same results.

Do I *have* to implement a DocumentPaginator to print a datagrid over several pages? You'd think there was some build in trick for such a common task?

S. Bro

        private void MenuItem_Print(object sender, RoutedEventArgs e)  
            PrintDialog printDialog = new PrintDialog();  
            if (printDialog.ShowDialog().GetValueOrDefault() == true)  
                ProjectPrintVisual printVisual = new ProjectPrintVisual();  
                printVisual.m_ProjectDataGridVisual.ItemsSource = m_AssignmentsView.m_ProjectsDataGrid.ItemsSource;  
                WriteXps(printVisual, printDialog);  
        public static void WriteXps(FrameworkElement visual, PrintDialog printDialog)  
            PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);  
            Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);  
            Size visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);  
            XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);  
            VisualsToXpsDocument vToXpsD = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator();  
            visual.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));  
            visual.Arrange(new Rect(new Point(0, 0), visual.DesiredSize));  
            double yOffset = 0;  
            while (yOffset < visual.DesiredSize.Height)  
                VisualBrush brush = new VisualBrush(visual);  
                brush.Stretch = Stretch.None;  
                brush.AlignmentX = AlignmentX.Left;  
                brush.AlignmentY = AlignmentY.Top;  
                brush.ViewboxUnits = BrushMappingMode.Absolute;  
                brush.TileMode = TileMode.None;  
                brush.Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height);  
                Canvas canvas = new Canvas();  
                canvas.Width = visibleSize.Width;  
                canvas.Height = visibleSize.Height;  
                canvas.Background = brush;  
                yOffset += visibleSize.Height;  
Mar 27, 2009 at 8:49 PM
Unfortunately we dont have proper support for printing with DataGrid. Yes, you will have to implement a DocumentPaginator to print a datagrid over several pages.
Aug 2, 2010 at 1:00 PM
Hi, I am trying to print a usercontrol in documentPaginator class. The user control has a datagrid in it. My problem starts that while printing to XPS document, datagrid is populated with data i send but when printing to a physical printer, i can only see blank rows in datagrid. My other fields on user- control are populated but only datagrid is creating an issue. Can u point out what mistake am i making or what other measures do I need to take care so that my datagrid prints properly on physical printer too.