Print GridView only in ASP.NET



Lets come back to ASP.NET tips and trick series. We are once again going to learn new trick related to GridView. Previously I have posted following articles related to GridView.


Suppose, you have a page where you are displaying any kind of data to user. You want to add a functionality to print these records. But problem with Browsers Default Print options (Ctrl+P) is that, it will print whole page. All header images, footer, sidebar and colors will printed, which is not required. You have two options, either create a new page for print view and redirect user to that page or just print the required area, in our case it is GridView.
Most of developer working in a web applications have MasterPage for their web-application. MasterPage are similar to templates in normal HTML having some advanced functionality and customized code for maintaining easier and common look of website or application.
Lets have a look at below screen to make our requirement more clear.
Print GridView only in ASP.NET


Code for Default.aspx

This is the code for GridView.

Code for Default.aspx.cs

We do not have much more at backend, we are just going to bind data to GridView.

Printing Page.

Lets press Ctrl+P key combination to print page, before printing see the Preview.
Print GridView only in ASP.NET


Printing GridView Only

We need to make following change to print GridView only. Lets start with HTML.

Wrap the GridView inside a Table or Panel. I am using Table. You can also go with Panel.

Now just add following JavaScript function in head section and assign that function to Print linkbutton.

Now lets click on Print Link button which we have created. Following will be output for above code.

Print GridView only in ASP.NET

Bingo! It was the requirement.
Hope you enjoyed reading. If you have any feedback or suggestions, please send us as comment or using contact options. Keep sharing.
John Bhatt

Article Name
Print GridView only in ASP.NET - PR's Blog
This article will tell you how to print gridview only in based page. When there is lots of image and css applied to design page , how to print data.

You might also like More from author