Introduction:
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.
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. |
|||
|
|||
39 comments :
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
How to create pagination for web page using JavaScript? Can you help me?
AS USUAL YOU ARE GREAT AND WORK AND THINKING IS TREMENDOUS..........U ROCKS DUDE
goooooooood woooooooooork
Can I have a question ? if I am using gridwiev page and I want to print all of the pages what I should do ?
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
Sir very nice artical but sir what we can do when grid-view have paging and we want to print whole data.
thanq u for this article...
Suresh, U r really great
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
Hi suresh..thanks a lot..it work :)
can you help to print the pdf files or images of a folder on single click of print button.
how to insert delete and print data in asp.net gridview, data of particular row,
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.
Hi.. how to print this Gridview in the page center?
Grid view Headers not visible ?
sir what we can do when grid-view have paging and we want to print whole data.
hellooooooooooooooooo
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.
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.
Sir please tell how to get the grid view data of a single row using java script.
hi u r articles are awesome sir
my gridview size is too long i want to resize my gridview and print it in a single page how to do it
If gridview has paging is it working .i m not sure
thanks.. very helpfull
Thanks a lot, this helped me
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
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.
u rock man..keep going.....
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
Awesome, Thanks
its nice ., but i need to show the name of the company in the top and show the footer as thanks
Can we remove one column from grid in print page?
Thanks to this. ;)
bt other click event on this page not working after fire the print event pls solve
nice artical
Thank you sooo much Suresh
Very Good. Thanks
Hello not work in repeter bind in asp.net c# plz. help me
Note: Only a member of this blog may post a comment.