How To Print GridView in Asp.Net
In this post we will learn How To Print GridView in Asp.Net. This feature come to handy for reporting purpose. Earlier we had discussed CRUD Operations Using Entity Framework in ASP.NET MVC. Here we are using JavaScript to accomplish this. We have used AdvetureWorks Database to fill GridView.
Design:
01
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02
03
<!DOCTYPE html>
04
05
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
06
<
head
runat
=
"server"
>
07
<
title
>How To Print GridView in Asp.Net</
title
>
08
<
style
type
=
"text/css"
>
09
body {
10
width: 980px;
11
margin: 0px auto;
12
text-align: center;
13
padding-top: 50px;
14
font-size: 20px;
15
}
16
</
style
>
17
<
script
type
=
"text/javascript"
>
18
function PrintGrid() {
19
var pGrid = document.getElementById('<%=gv.ClientID %>');
20
pGrid.border = 0;
21
var pwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
22
pwin.document.write(pGrid.outerHTML);
23
pwin.document.close();
24
pwin.focus();
25
pwin.print();
26
pwin.close();
27
}
28
</
script
>
29
</
head
>
30
<
body
>
31
<
form
id
=
"form1"
runat
=
"server"
>
32
<
div
>
33
<
h1
>How To Print GridView in Asp.Net</
h1
>
34
<
asp:GridView
ID
=
"gv"
runat
=
"server"
AllowPaging
=
"true"
PageSize
=
"5"
ShowFooter
=
"true"
AutoGenerateColumns
=
"false"
35
Width
=
"100%"
DataSourceID
=
"sdsrc"
OnRowDataBound
=
"gv_RowDataBound"
>
36
37
<
Columns
>
38
<
asp:BoundField
DataField
=
"ProductID"
HeaderText
=
"Product ID"
/>
39
<
asp:BoundField
DataField
=
"PurchaseOrderDetailID"
HeaderText
=
"Purchase Order ID"
/>
40
<
asp:TemplateField
HeaderText
=
"Order Quantity"
>
41
<
ItemTemplate
>
42
<
asp:Label
ID
=
"lblOrderQty"
runat
=
"server"
Text='<%#Eval("OrderQty") %>' />
43
</
ItemTemplate
>
44
<
FooterTemplate
>
45
<
asp:Label
ID
=
"lbltxtTotal"
runat
=
"server"
Text
=
"Total Price"
/>
46
</
FooterTemplate
>
47
</
asp:TemplateField
>
48
<
asp:TemplateField
HeaderText
=
"Price"
>
49
<
ItemTemplate
>
50
<
asp:Label
ID
=
"lblPrice"
runat
=
"server"
/>
51
</
ItemTemplate
>
52
<
FooterTemplate
>
53
<
asp:Label
ID
=
"lblTotal"
runat
=
"server"
/>
54
</
FooterTemplate
>
55
</
asp:TemplateField
>
56
</
Columns
>
57
</
asp:GridView
>
58
59
<
asp:SqlDataSource
ID
=
"sdsrc"
runat
=
"server"
SelectCommand
=
"select * from [AdventureWorks2008R2].[Purchasing].[PurchaseOrderDetail]"
ConnectionString="<%$ ConnectionStrings:con %>">
60
</
asp:SqlDataSource
>
61
62
<
br
/><
br
/>
63
<
input
type
=
"button"
onclick
=
"PrintGrid()"
value
=
"Print"
/>
64
<
br
/><
br
/>
65
All rights reserved by <
a
href
=
"http://www.hightechnology.in"
>www.Hightechnology.in</
a
> |
66
Hosting partner <
a
href
=
"http://www.grootstech.com"
target
=
"_blank"
>Grootstech</
a
>
67
</
div
>
68
</
form
>
69
</
body
>
70
</
html
>
Result:
01 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
02 |
03 | <!DOCTYPE html> |
04 |
05 | < html xmlns = "http://www.w3.org/1999/xhtml" > |
06 | < head runat = "server" > |
07 | < title >How To Print GridView in Asp.Net</ title > |
08 | < style type = "text/css" > |
09 | body { |
10 | width: 980px; |
11 | margin: 0px auto; |
12 | text-align: center; |
13 | padding-top: 50px; |
14 | font-size: 20px; |
15 | } |
16 | </ style > |
17 | < script type = "text/javascript" > |
18 | function PrintGrid() { |
19 | var pGrid = document.getElementById('<%=gv.ClientID %>'); |
20 | pGrid.border = 0; |
21 | var pwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1'); |
22 | pwin.document.write(pGrid.outerHTML); |
23 | pwin.document.close(); |
24 | pwin.focus(); |
25 | pwin.print(); |
26 | pwin.close(); |
27 | } |
28 | </ script > |
29 | </ head > |
30 | < body > |
31 | < form id = "form1" runat = "server" > |
32 | < div > |
33 | < h1 >How To Print GridView in Asp.Net</ h1 > |
34 | < asp:GridView ID = "gv" runat = "server" AllowPaging = "true" PageSize = "5" ShowFooter = "true" AutoGenerateColumns = "false" |
35 | Width = "100%" DataSourceID = "sdsrc" OnRowDataBound = "gv_RowDataBound" > |
36 |
37 | < Columns > |
38 | < asp:BoundField DataField = "ProductID" HeaderText = "Product ID" /> |
39 | < asp:BoundField DataField = "PurchaseOrderDetailID" HeaderText = "Purchase Order ID" /> |
40 | < asp:TemplateField HeaderText = "Order Quantity" > |
41 | < ItemTemplate > |
42 | < asp:Label ID = "lblOrderQty" runat = "server" Text='<%#Eval("OrderQty") %>' /> |
43 | </ ItemTemplate > |
44 | < FooterTemplate > |
45 | < asp:Label ID = "lbltxtTotal" runat = "server" Text = "Total Price" /> |
46 | </ FooterTemplate > |
47 | </ asp:TemplateField > |
48 | < asp:TemplateField HeaderText = "Price" > |
49 | < ItemTemplate > |
50 | < asp:Label ID = "lblPrice" runat = "server" /> |
51 | </ ItemTemplate > |
52 | < FooterTemplate > |
53 | < asp:Label ID = "lblTotal" runat = "server" /> |
54 | </ FooterTemplate > |
55 | </ asp:TemplateField > |
56 | </ Columns > |
57 | </ asp:GridView > |
58 | |
59 | < asp:SqlDataSource ID = "sdsrc" runat = "server" SelectCommand = "select * from [AdventureWorks2008R2].[Purchasing].[PurchaseOrderDetail]" ConnectionString="<%$ ConnectionStrings:con %>"> |
60 | </ asp:SqlDataSource > |
61 |
62 | < br />< br /> |
63 | < input type = "button" onclick = "PrintGrid()" value = "Print" /> |
64 | < br />< br /> |
65 | All rights reserved by < a href = "http://www.hightechnology.in" >www.Hightechnology.in</ a > | |
66 | Hosting partner < a href = "http://www.grootstech.com" target = "_blank" >Grootstech</ a > |
67 | </ div > |
68 | </ form > |
69 | </ body > |
70 | </ html > |