Home > Forum > ASP.NET > How to print particular record data through gridview imagebutton using asp.net

How to print particular record data through gridview imagebutton using asp.net

by Thamil   on Oct 15, 2014   Category: ASP.NET  |  Views: 2991    |  Points: 5   |  Gold 

I just want to print particular data record which is shown in grid view on clicking grid view image button.
It should directly open print popup (Preview print form)

Question posted on behalf of shubham bhardwaj

Bookmark and Share:
  User Reply  | Ask a question  |   Reply 
  Re :How to print particular record data through gridview imagebutton using asp.net   
by Thamil
on Oct 15 2014 5:37AM
Points : 10
Hi shubham bhardwaj,

Below sample code will print the specific record when you click on print button in the gridview.
I have loaded the some test data with gridview by using template field then placed the controls inside the div tag in the itemtemplate. See the below sample code

ASPX page:
 <asp:GridView ID="GVEmp" AutoGenerateColumns="False" runat="server" OnRowDataBound="GVEmp_RowDataBound">
<asp:TemplateField HeaderText="Name" HeaderStyle-Width="500px">
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" Height="25px" />
<div id="printRow" runat="server">
<asp:Label ID="lblReason" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
<asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
<asp:TemplateField HeaderText="Print" HeaderStyle-Width="100px">
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
<asp:ImageButton ID="imgBtnPrint" ImageUrl="~/Print.jpg" runat="server" />

<script language="javascript" type="text/javascript">
function printPage(div)
var data = document.getElementById(div).innerHTML;
var mywindow = window.open('', '_blank', 'height=400,width=600');
mywindow.document.write('<html><body>' + data + '</body></html>');

Code behind part:
protected void Page_Load(object sender, EventArgs e)
GVEmp.DataSource = GetData();

private DataTable GetData()
DataTable dtEmp = new DataTable("Emp");
DataColumn Name = new DataColumn("Name", typeof(System.String));
DataColumn Dept = new DataColumn("Department", typeof(System.String));
dtEmp.Columns.AddRange(new DataColumn[] { Name, Dept});
dtEmp.Rows.Add("Ricki", "Information Technoloy");
dtEmp.Rows.Add("Loretta", "Finance Control");
dtEmp.Rows.Add("Hassen", "Customer Care");
dtEmp.Rows.Add("Maddox", "Customer Care");
dtEmp.Rows.Add("Sheldon", "Finance Control");
dtEmp.Rows.Add("Aadinath", "Customer Care");
dtEmp.Rows.Add("Aakaash", "Information Technoloy");
dtEmp.Rows.Add("Aamod", "Customer Care");
return dtEmp;
protected void GVEmp_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
HtmlGenericControl div = e.Row.FindControl("printRow") as HtmlGenericControl;
string DivID = div.ClientID;
ImageButton btnPrint = (ImageButton)e.Row.FindControl("imgBtnPrint");
btnPrint.Attributes.Add("OnClick", "printPage('"+DivID+"');");

I'm using javascript function to print the content of div tag. In gridview rowdatabound event, i'm getting div tag client id and passing the javascript function when clicks on imagebutton.

Please let me know if you have any question on this.

Recent Post

Latest Posts