Printing GridView Only in ASP.NET

Let's return to the ASP.NET tips and trick series. We are once again going to learn a new trick related to GridView. Previously I posted the following articles related to GridView.

Requirements

Suppose you have a page where you are displaying some data to the user. You want to add functionality to print these records. But the problem with the Browsers Default Print options (Ctrl+P) is that, it will print an entire page. All header images, footers, sidebars and colors will be printed, which is not required. You have two options, either create a new page for a print view and redirect the user to that page or just print the required area, in our case it is the GridView.

Most developers working in a web applications have a MasterPage for their web application. A MasterPage is similar to templates in normal HTML having some advanced functionality and customized code for maintaining an easier and common look of a website or application.

Let's have a look at the following screen to make our requirements more clear.

MasterPage

Code for Default.aspx

This is the code for the GridView. 

  1. <asp:GridView runat="server" ID="gvProducts" AutoGenerateColumns="false" AllowPaging="false"  
  2.     AlternatingRowStyle-BackColor="Linen" HeaderStyle-BackColor="SkyBlue" Width="100%"  
  3.     OnPageIndexChanging="gvProducts_PageIndexChanging" EmptyDataText="Sorry! No Products to List. First Add from Add Product Link.">  
  4.     <Columns>  
  5.         <asp:TemplateField HeaderText="Product ID">  
  6.             <ItemTemplate>  
  7.                 <asp:Label ID="lblProductID" runat="server" Text='<%#Eval("ProductID")%>' ToolTip="ID of Product as stored in Database."></asp:Label>  
  8.             </ItemTemplate>  
  9.         </asp:TemplateField>  
  10.     </Columns>  
  11.     <Columns>  
  12.         <asp:TemplateField HeaderText="Product Name">  
  13.             <ItemTemplate>  
  14.                 <asp:Label ID="lblProductName" runat="server" ToolTip="Name of Product" Text='<%#Eval("ProductName")%>'></asp:Label>  
  15.             </ItemTemplate>  
  16.         </asp:TemplateField>  
  17.     </Columns>  
  18.     <Columns>  
  19.         <asp:TemplateField HeaderText="Brand">  
  20.             <ItemTemplate>  
  21.                 <asp:Label ID="lblBrandName" runat="server" ToolTip="Brand of Product" Text='<%#Eval("BrandName")%>'></asp:Label>  
  22.             </ItemTemplate>  
  23.         </asp:TemplateField>  
  24.     </Columns>  
  25.     <Columns>  
  26.         <asp:TemplateField HeaderText="Category">  
  27.             <ItemTemplate>  
  28.                 <asp:Label ID="lblProductCat" runat="server" ToolTip="Category of Product" Text='<%#Eval("CategoryName")%>'></asp:Label>  
  29.             </ItemTemplate>  
  30.         </asp:TemplateField>  
  31.     </Columns>  
  32.     <Columns>  
  33.         <asp:TemplateField HeaderText="In Stock">  
  34.             <ItemTemplate>  
  35.                 <asp:Label ID="lblProductinStock" runat="server" ToolTip="Quantity available in Stock"  
  36.                     Text='<%#Eval("UnitsInStock")%>'></asp:Label>  
  37.             </ItemTemplate>  
  38.         </asp:TemplateField>  
  39.     </Columns>  
  40. </asp:GridView> 

Code for Default.aspx.cs

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

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToStore"].ConnectionString);  
  6.         SqlDataAdapter adp = new SqlDataAdapter("Select * from Products,Brands,Category where Products.BrandID=Brands.BrandID and Products.CategoryID=Category.CategoryID", con);  
  7.         DataSet ds = new DataSet();  
  8.         adp.Fill(ds);  
  9.         gvProducts.DataSource = ds.Tables[0];  
  10.         gvProducts.DataBind();  
  11.     }  
  12. }

Printing the Page

Let's press the Ctrl+P key combination to print the page, before printing see the Preview.

Printing Page

Printing GridView Only

We need to make the following change to print the GridView only. Let's start with the HTML.

Wrap the GridView inside a Table or Panel. I am using a table. Alternatively you can use a Panel.
  1. <table width="70%" id="pnlGridView" runat="server" align="center" class="ContentTable">  
  2.     <tr>  
  3.         <td colspan="2" align="center">  
  4.             <h1>  
  5.                 All Products in Store</h1>  
  6.         </td>  
  7.     </tr>  
  8.     <tr>  
  9.         <td>  
  10.         </td>  
  11.     </tr>  
  12.     <tr>  
  13.         <td colspan="2">  
  14.             <asp:gridview runat="server" id="gvProducts" autogeneratecolumns="false" allowpaging="false"  
  15.                 alternatingrowstyle-backcolor="Linen" headerstyle-backcolor="SkyBlue" width="100%"  
  16.                 onpageindexchanging="gvProducts_PageIndexChanging" emptydatatext="Sorry! No Products to List. First Add from Add Product Link.">  
  17.                 ...............  
  18.                 .............  
  19.             </asp:gridview>  
  20.         </td>  
  21.     </tr>  
  22.     <tr>  
  23.         <td align="right">  
  24.             <asp:linkbutton id="lnkPrint" runat="server" tooltip="Click to Print All Records"  
  25.                 text="Print Data" onclick="lnkPrint_Click">   
  26.             </asp:linkbutton>  
  27.             <asp:linkbutton id="lnkExportAll" runat="server" tooltip="Export this List" text="Export to Excel"  
  28.                 onclick="lnkExportAll_Click"></asp:linkbutton>  
  29.             <asp:linkbutton id="lnkAddNew" runat="server" tooltip="Add New Product" text="Add New"  
  30.                 onclick="lnkAddNew_Click"></asp:linkbutton>  
  31.         </td>  
  32.     </tr>  
  33. </table>  

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

  1. <script language="javascript" type="text/javascript">  
  2.     function PrintPage() {  
  3.         var printContent = document.getElementById('<%= pnlGridView.ClientID %>');  
  4.         var printWindow = window.open("All Records""Print Panel"'left=50000,top=50000,width=0,height=0');  
  5.         printWindow.document.write(printContent.innerHTML);  
  6.         printWindow.document.close();  
  7.         printWindow.focus();  
  8.         printWindow.print();  
  9.     }  
  10. </script> 

Now let's click on the Print Link button that we created. The following will be the output of the code above.

Printing GridView Only

Bingo! It satisfies the requirements.

I hope you enjoyed reading this. If you have any feedback or suggestions then please send us as comment or use the contact options. Keep sharing.

To read more blogs: P.R's Blog


Similar Articles