How to print the contents of a GridView

Hi Experts,

I have a web application with C# as the code behind language which pulls data from a DB table and displays it in a GridView.
The data is pretty large and  runs to around 50 pages when paging is allowed in the GridView. It makes to sense to print each page by using the browser's print functionality.
How do I write a customized print function which prints all the GridView's records ?

Who is Participating?
krys2ferConnect With a Mentor Commented:
You can create a new page with the GridView minus the paging alone and pop it during printing.
You can get really good control of printing by using CSS.
Have you tried something like this?
using System.Drawing.Printing;

protected void Button1_Click()
        PrintDocument pd = new PrintDocument();

        foreach(Page page in GridView1.Page)

Open in new window

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Kumaraswamy RCommented:

Bellow url Give some information

how to print data in a DataGridView, please visit the following links:
Hi Dear..

Please add this button on html
<asp:Button CssClass="button" ID="btnPrnt" runat="server" Text="Print Preview" />

Add in aspx.cs Page on page load
btnPrnt.Attributes.Add("Onclick", "getPrint('print');")

Do more thing add a one more grid with full record and visible =false
<div id="print">
Your grid here

fantasylanAuthor Commented:
Hi apexexpert,

I tried what you mentioned but I am printing just a blank page. I also made the GridVew visible to make sure that there was data in it.

Any ideas on what I might be doing wrong ?

fantasylanAuthor Commented:
Hi russellC,

The code you posted didnt work for me as I got an Ienumerable error.

fantasylanAuthor Commented:
Hi rkworlds,

I am looking to print a GridView and not a DataGridView. I am not sure the tutorials that you pointed out would work as they are customized to windows forms rather than web forms.

please check  your enableviewstate="true" for gridview

sometimes it happens due to visible false so you can try one more thing just check your view source have it contains the data or grid view.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.