Wednesday, March 20, 2013

Print Grid View Data Using Java Script C# Asp .Net

==>> Create a table in SQL Employee and create a website in add new page  and paste the Code:--

1.  ASPX PAGE CODE:-

 <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Print Gridview Data</title>
<script type="text/javascript">
function Print() {
var PrintGrid = document.getElementById('<%=EmpGrid.ClientID %>');
PrintGrid.border = 0;
var PrintGridWindow = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
PrintGridWindow.document.write(PrintGrid.outerHTML);
PrintGridWindow.document.close();
PrintGridWindow.focus();
PrintGridWindow.print();
PrintGridWindow.close();
}
</script>
</head>
<body>
<form id="FmGridPrint" runat="server">
<div>
<b>Print Gridview</b><br /><br />
<asp:GridView ID="EmpGrid" runat="server" >

</asp:GridView>
<input type="button" id="btnPrint" value="Print" onclick="Print()" />
</div>
</form>
</body>
</html>


2. ASPX.CS PAGE CODE:-



using System;
using System.Data;
using System.Data.SqlClient;


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}


protected void BindGrid()
{
using (SqlConnection con = new SqlConnection("Initial Catalog=EmployeeTest;Data Source=SUBHASH;Integrated Security=true"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Employee", con);
SqlDataReader dr = cmd.ExecuteReader();
EmpGrid.DataSource = dr;
EmpGrid.DataBind();
con.Close();
}
}


No comments:

Post a Comment