In this article I am going to explain how we can print
a GridView data.
Below will be output of my program..

Image 1.
Click on Print Button..

Image 2.
aspx for this application..
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Print Gridview Data in asp.net</title>
<script type="text/javascript">
function MakePrintGrid() {
var prntMyGrid = document.getElementById('<%=GridEmployee.ClientID %>');
prntMyGrid.border = 0;
var MakePrntMyGrid = window.open('',
'MakePrintGrid', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
MakePrntMyGrid.document.write(prntMyGrid.outerHTML);
MakePrntMyGrid.document.close();
MakePrntMyGrid.focus();
MakePrntMyGrid.print();
MakePrntMyGrid.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="20"
cellspacing="20"
width="50%"
align="center"
border="2">
<tr>
<td align="center">
<asp:GridView ID="GridEmployee"
runat="server"
CellPadding="8"
CellSpacing="1"
ForeColor="#333333"
GridLines="None">
<RowStyle
BackColor="#F7F6F3"
ForeColor="#333333"
/>
<FooterStyle
BackColor="#5D7B9D"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle
BackColor="#284775"
ForeColor="White"
HorizontalAlign="Center"
/>
<SelectedRowStyle
BackColor="#E2DED6"
Font-Bold="True"
ForeColor="#333333"
/>
<HeaderStyle
BackColor="#5D7B9D"
Font-Bold="True"
ForeColor="White"
/>
<EditRowStyle
BackColor="#999999"
/>
<AlternatingRowStyle
BackColor="White"
ForeColor="#284775"
/>
</asp:GridView>
</td>
</tr>
<tr>
<td align="center">
<input type="button"
id="btnPrint"
value="Print"
onclick="MakePrintGrid()"
/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
aspx.cs code is:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class _Default :
System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void
BindData()
{
SqlConnection con = new
SqlConnection(@"Server=.;Database=Test;Integrated
Security=true;");
SqlCommand cmd = new
SqlCommand("SELECT * FROM Employee", con);
try
{
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
if (!object.Equals(ds.Tables[0],
null))
{
if (ds.Tables[0].Rows.Count > 0)
{
GridEmployee.DataSource = ds.Tables[0];
GridEmployee.DataBind();
Session["MyTable"] =
ds.Tables[0];
}
else
{
GridEmployee.DataSource = null;
GridEmployee.DataBind();
}
}
else
{
GridEmployee.DataSource = null;
GridEmployee.DataBind();
}
}
catch (Exception
ex)
{
}
}
}