Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Print Data of GridView

Posted by Viral Sarvaiya on July 8, 2009

here is the steps:

1) create simple web application (here i used asp.net with C#).

2) put following control in to Page.

1) Gridview (id = GridView1)

2) html button (id =Button1)

<inputid=”Button1″type=”button”value=”Print”language=”javascript”onclick=”return Button1_onclick()” />
<asp:GridViewID=”GridView1″runat=”server”>
</asp:GridView>

3)  fill data into GridView

//here i used the list you  can bind with database also

System.Collections.Generic.List<string> obj = newSystem.Collections.Generic.List<string>();

for(inti = 0; i < 10; i++)
{
obj.Add(i.ToString());
}
GridView1.DataSource = obj;
GridView1.DataBind();

4) now write the print function. click on the Button1 or write Java Script.

<script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
//open new window set the height and width =0,set windows position at bottom
var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0');
//write gridview data into newly open window
a.document.write(document.getElementById('<%= GridView1.ClientID %>').innerHTML);
a.document.close();
a.focus();
//call print
a.print();
a.close();
return false;
}
// -->
</script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: