Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Print Asp.net Gridview Data using C# VB.NET JavaScript

Oct 17, 2012
Introduction

Here I will explain how to print gridview data in asp.net using C# and VB.NET.

Description:
  
In previous posts I explained Get Gridivew hidden field value in asp.net, jQuery Check uncheck all checkboxes in gridview, Best jQuery Drag and Drop Plugins and many articles relating to gridview, JQuery. Now I will explain how to print gridview data in asp.net using C# and VB.NET.

Before implement this example first design one table UserInformation in your database as shown below

Column Name
Data Type
Allow Nulls
UserId
Int (Set Identity=true)
No
UserName
varchar(0)
Yes
Location
Varchar(50)
Yes
Once table designed enter some dummy data for our sample after that write the following code in Default.aspx page like as shown below


<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('<%=gvUserInfo.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="gvUserInfo" 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>
Now add following namespaces in codebehind

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
After that add following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database

protected void BindGridview()
{
using (SqlConnection con = new SqlConnection("Initial Catalog=MySampleDB;Data Source=SureshDasari;Integrated Security=true"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select TOP 10 UserId,UserName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
gvUserInfo.DataSource = dr;
gvUserInfo.DataBind();
con.Close();
}
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is used to bind gridview from database
Protected Sub BindGridview()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select UserName,LastName,Location from UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
End Class
Demo


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

39 comments :

rifayee said...

hi suresh,,,,this is article is very useful for me..just i want to print the particular record in the gridview..i provide a print option for row,,if i need to take a print out for a particular record,how can i do that pls tell me..suresh

Muthusaravanan said...

How to create pagination for web page using JavaScript? Can you help me?

Abhinavsingh993 said...

AS USUAL YOU ARE GREAT AND WORK AND THINKING IS TREMENDOUS..........U ROCKS DUDE

Anonymous said...

goooooooood woooooooooork

Anonymous said...

Can I have a question ? if I am using gridwiev page and I want to print all of the pages what I should do ?

Anonymous said...

hi thank you for this article
i have question
i want to print gridview multi pages
and after 10 rows have page break
to prevent rows goews to next page
my email is devnet84@yahoo.com
thank you

Unknown said...

Sir very nice artical but sir what we can do when grid-view have paging and we want to print whole data.

Anonymous said...

thanq u for this article...

Anonymous said...

Suresh, U r really great

kannan said...

Hi suresh
i need one help please.i want to print in PDF file in firefox.chrome,IE is print option working but Firefox not working please can you help me.
am using C#.net,scripting function.
am using iframe.
print the full window in firefox not displaying content please help

Anonymous said...

Hi suresh..thanks a lot..it work :)

Anonymous said...

can you help to print the pdf files or images of a folder on single click of print button.

Unknown said...

how to insert delete and print data in asp.net gridview, data of particular row,

char said...

Hi I am not getting the grdview ID .I am using this line to get grid ID.
document.getElementById("gridrecord");.Kinly help me in this matter.

Kirthi said...

Hi.. how to print this Gridview in the page center?

kartheeswaran Jeyakumar said...

Grid view Headers not visible ?

Anonymous said...

sir what we can do when grid-view have paging and we want to print whole data.

Anonymous said...

hellooooooooooooooooo

Anonymous said...

Hi.....Actually i tried your code to print out the gridview data it's working fine but problem is that it's printing the gridview data with Edit, Update,Delete button i want to print gridview data without buttons. if you have any clue or code plz send me.

Unknown said...

Hi.....Actually i tried your code to print out the gridview data it's working fine but problem is that it's printing the gridview data with Edit, Update,Delete button i want to print gridview data without buttons. if you have any clue or code plz send me.

Unknown said...

Sir please tell how to get the grid view data of a single row using java script.

Anonymous said...

hi u r articles are awesome sir

Anonymous said...

my gridview size is too long i want to resize my gridview and print it in a single page how to do it

sameer said...

If gridview has paging is it working .i m not sure

vikash said...

thanks.. very helpfull

Anonymous said...

Thanks a lot, this helped me

suresh said...

hi suresh when i am selecting different dropdownlists after that i getting gridview,i want to now print the gridview with,what i selected above options also clearly display in that page,i want code please send me

Anonymous said...

Hello sir,
This is kohila.....
I'm your fan man.......
Awesome your each code was very fantastic..
Superb.. Keep coding..
Thank you for these code.

Aarish Shaik said...

u rock man..keep going.....

Unknown said...

hi sir,
Thank you for this posts.
I have one question, In my page there are two grid view and i want to print the both grid view record on single page then how can i print that please help me for this...
thank You

Anonymous said...

Awesome, Thanks

Anonymous said...

its nice ., but i need to show the name of the company in the top and show the footer as thanks

Nidhi said...

Can we remove one column from grid in print page?

IamSam said...

Thanks to this. ;)

Anonymous said...

bt other click event on this page not working after fire the print event pls solve

Anonymous said...

nice artical

Unknown said...

Thank you sooo much Suresh

Moises Rovani de Andrade said...

Very Good. Thanks

Unknown said...

Hello not work in repeter bind in asp.net c# plz. help me

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.