ASP.net How to Print Only GridView
<div id="gridviewDiv">
<asp:GridView ID="gridViewToPrint" runat="server"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID"/>
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName"/>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="btnPrint" runat="server"
Text="Print GridView Data"/>
protected void Page_Load(object sender, EventArgs e)
{
string printScript =
@"function PrintGridView()
{
var gridInsideDiv = document.getElementById('gridviewDiv');
var printWindow = window.open('gridview.htm','PrintWindow','letf=0,top=0,width=150,height=300,toolbar=1,scrollbars=1,status=1');
printWindow.document.write(gridInsideDiv.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();}";
this.ClientScript.RegisterStartupScript(Page.GetType(), "PrintGridView", printScript.ToString(), true);
btnPrint.Attributes.Add("onclick", "PrintGridView();");
}
No comments:
Post a Comment