Print DataGridView

Posted on 2006-05-18
Last Modified: 2008-01-09
I have a c# windows program which will query data from database, and show it on the dataGridView
I want to create a button so that I can click on the button to print the dataGridView or dataTable
how can I do that?

Question by:Vik Shah
    LVL 11

    Expert Comment

    you will have to do all your drawing by yourself using the graphics object if a printdocument.
    to get more information, which objects you need to get to the point, where you have to to your drawing, take a look at this link:

    Author Comment

    by:Vik Shah
    I check the link, I have a similar c# code sample from same editor (Michael Gold)
     I  look at the code, it uses DataGrid instead of DataGridView
    and DataSet instead of DataTable. I modify the code to use DataGridView and DataTable.

    The structure of my program is :
    Several DataGridViews in a  windows form ,
    Each of them has a DataTable as DataSource
    Next to each view, there is a print button
    the print button will call
                dataGridPrinter1 = new DataGridPrinter(DataGridView1, printDocument1, DataTable1);
                dataGridPrinter1.PageNumber = 1;
                dataGridPrinter1.RowCount = 0;
                if (printDialog1.ShowDialog() == DialogResult.OK)

    but when I try to preview or print, it only shows a blank page.

    What could possibly go wrong?

    Thanks a lot

    By the way,  are you Michael Gold ?
    LVL 11

    Assisted Solution

    no, im not michael gold :)
    you only got a blanc page because you did not draw your grid. you must
    use the printpage event of the printdocument like this and make your drawing of all the grid data
    in his example, it looks like that:

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      Graphics g = e.Graphics;
    // Draw  a label title for the grid

    // draw the datagrid using the DrawDataGrid method passing the Graphics surface
      bool more = dataGridPrinter1.DrawDataGrid(g);

    // if there are more pages, set the flag to cause the form to trigger another print page event
      if (more == true)
         e.HasMorePages = true;

    DrawTopLabel and dataGridPrinter1.DrawDataGrid are some methods, which draws all the data into the printdocument graphics obect, in hsi example called 'g'.
    so you will have to implement that methods too.
    LVL 8

    Expert Comment

    Try out to use Microsoft Reporting Services.
    It gives you a lot of flexibility and you don't need to paint it yourself.
    LVL 2

    Accepted Solution

    I posted a C# class to print a DataGridView, have a look to the article, class (.cs) file, and a demo project in the following url:

    Hope it will help you..


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now