How To: Easy print and print preview with RadGridView for WPF

by Vladimir Enchev | Comments 5

Using FixedDocument, DocumentViewer and PrintDialog you can easily create your own print and/or print preview for RadGridView for WPF.

Untitled

I’ve made two extension methods Print() and PrintPreview() to illustrate how to turn the grid into a print friendly document in few lines of code:

public static void PrintPreview(this GridViewDataControl source)
{
    var window = new Window()
    {
        Title = "Print Preview",
        Content = new DocumentViewer()
        {
            Document = ToFixedDocument(ToPrintFriendlyGrid(source), new PrintDialog())
        }
    };

    window.ShowDialog();
}

public static void Print(this GridViewDataControl source, bool showDialog)
{
    var dialog = new PrintDialog();
    var dialogResult = showDialog ? dialog.ShowDialog() : true;

    if (dialogResult == true)
    {
        var viewer = new DocumentViewer();
        viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), dialog);
        dialog.PrintDocument(viewer.Document.DocumentPaginator, "");
    }
}

,
Senior Technical Architect

5 Comments

Daniel Thomas
This is amazing, thanks. Is it possible for us to somehow export this rather than printing it using PdfFormatProvider, I am currently exporting as shown in the demos but I much prefer the look of these printouts.
Erik
When my grid is wider than the page width, it is cut off in the print preview. How can I print the entire grid?
How can I add margins to the side of the page?
mk
This is very good sample. I noticed the printing takes long time to print because it is going and adjusting all the elements to infinity size. Wonder how to speed it up? Is there any async way to do this? Really appreciate your help. Thanks.
mk
This is very good sample. I noticed the printing takes long time to print because it is going and adjusting all the elements to infinity size. Wonder how to speed it up? Is there any async way to do this? Really appreciate your help. Thanks.
mk
This is very good sample. I noticed the printing takes long time to print because it is going and adjusting all the elements to infinity size. Wonder how to speed it up? Is there any async way to do this? Really appreciate your help. Thanks.

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by Vladimir Enchev - or - read latest articles in Developer Tools
Product Families