Publish Article

How to print Data Grid or GridView


If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.

This article will discuss how you can use our PrintLib.Net and ActivePdf server to create PDF reports of the data displayed in your grid. The article can help answer some frequently asked questions like:

  • How to print GridView?
  • How to print DataGrid?
  • How to create PDF report from a DataSet?
  • How to print ASP.Net page as PDF document?
  • Create PDF report from ASP.Net page
  • ... and more

A very common method used by lot of web developers for printing pages or data rendered on the pages is by asking user to hit "Print" button on browser. And almost all users do not like the print outs because they contain menus, page footers, headers and other junk that user does not really care about. The user wants a nice looking report printed for them. Grid is most commonly used way to display tabular data. And you want to print the records in nice format and ability to have grid header on each page when data spans more than one page. To solve these very problems we created a printing library that allows us to specify layout for the report or grid and then use a print driver to render the report and then stream is back to client.

We have used ActivePDF server as print driver and PrintLib.Net for specifying layput of report. You can download these 2 libraries from the following links. ActivePDF offers evaluation copy of their product to test. And as usual PrintLib.Net has a trial edition which you can use as long as you desire (with limited capabilities).

For our demo project we have setup a grid view with following columns.

<asp:GridView ID="ctlGridView" runat="server" autogeneratecolumns=False gridlines=Horizontal>
        <asp:boundfield datafield="ProductID" sortexpression="ProductID" />
        <asp:boundfield datafield="Name" headertext="Name" sortexpression="Name" />
        <asp:boundfield datafield="ProductNumber" headertext="Product Number" />
        <asp:BoundField DataField="ListPrice" HeaderText="List Price" DataFormatString="{0:c}" />
        <asp:BoundField DataField="SellStartDate" HeaderText="Sell Start Date" DataFormatString="{0:d}" />

The following code is from the event handler that fires when user clicks on "Print" button on grid view.

Handling Print Button Click

protected void OnPrint(object sender, EventArgs e)
    m_obPrtMgr = new PrintManager();

    PRT2DISKLib.Print2DiskClass pdfServer = new PRT2DISKLib.Print2DiskClass();
    String strPdfFile = Server.MapPath("~/PdfReports");
    pdfServer.PDFAuthor = "Winista";
    pdfServer.OutputDirectory = strPdfFile;
    pdfServer.NewDocumentName = Guid.NewGuid().ToString("N") + ".pdf";
    strPdfFile = System.IO.Path.Combine(strPdfFile, pdfServer.DocumentName);
    pdfServer.GeneralFlags += 16;

    PrintDocument doc = new PrintDocument();
    doc.PrintPage += new PrintPageEventHandler(this.Print);
    pdfServer.PrintQuality = -4;	// High resolution
    int iRetVal = pdfServer.StartPrinting();
    doc.PrinterSettings.PrinterName = pdfServer.NewPrinterName;
    strPdfFile = "/PdrRefports/" + pdfServer.DocumentName;
    pdfServer = null;

Setting up grid printing layout

Following code from demo project show how we used PrintLib.Net library to set the layout for printing the data from grid view.

private PrintDataTable PrepareDataTable(DataTable dtable)
    PrintDataTable dt = new PrintDataTable();
    dt.DrawTableHeader = true;
    dt.DataSource = dtable.DefaultView;

    // Add column information.
    PrintDataColumnCollection cols = dt.Columns;

    PrintDataColumn col = new PrintDataColumn("ProductID", 1.25f, StringAlignment.Far);
    col.HeaderBackColor = Color.Gainsboro;
    col.BackColor = Color.Aquamarine;
    col.HeaderText = "Product ID";
    col.HeaderAlignment = StringAlignment.Center;

    col = new PrintDataColumn("Name", 1.25f, StringAlignment.Near);
    col.HeaderBackColor = Color.Gainsboro;
    col.ForeColor = Color.Blue;
    col.HeaderText = "Product Name";
    col.HeaderAlignment = StringAlignment.Center;

    col = new PrintDataColumn("ListPrice", 1.25f, StringAlignment.Far);
    col.HeaderText = "List Price";
    col.HeaderBackColor = Color.Gainsboro;
    col.HeaderAlignment = StringAlignment.Center;
    col.FormatDataColumn += new FormatDataColumnEventHandler(this.FormatDateColumn);

    return dt;


You can click on the following link to download the PDF file that was generated by the demo project using trial version of PrintLib.Net and ActivePDF server.

If you run this demo project make sure that you have popup blocker disabled because the sample tries to open the generated PDF document using javascript window.open method.

Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix