Print Gridview Data in asp.net using javascript.

Bageshkumarbagi
Posted by Bageshkumarbagi under ASP.NET category on | Points: 40 | Views : 4971
======html asp.net code========

<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 PrintGridData() {
var prtGrid = document.getElementById('<%=gv.ClientID %>');
prtGrid.border = 0;
var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML);
prtwin.document.close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div><b>Print Gridview Data</b>
<br />
<br /><asp:GridView ID="gv" runat="server" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White"/></asp:GridView>
<input type="button" id="btnPrint" value="Print" onclick="PrintGridData()" />
</div>
</form>
</body>
</html>

==========C# code================

using System;
using System.Collections;
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.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
SqlConnection con = new SqlConnection("Data Source=SYNCGDC0491;Initial Catalog=ReportServer;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select TOP 20 id,name,location from emp", con);
SqlDataReader dr = cmd.ExecuteReader();
gv.DataSource = dr;
gv.DataBind();
con.Close();
}
catch (Exception ea)
{
Response.Write(ea.Message);
}
}
}
}

=======database design===========
table name is emp
id int
name varchar(50)
location varchar(50)

Comments or Responses

Login to post response