vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, April 4, 2012

Take Print only gridview data in asp.net


Introduction:
Here you will learn to print only grid view content in asp.net web page.

In a previous article I wrote to print a web page for you reference I given the following below..


Some case we need to print only grid view data, In that case we follow this method code..
Here I kept a grid view inside the panel, so when print we call directly the panel id to print..
First :
We have to create data table to bind in grid view.
Code:
DataTable dt=new DataTable();
    DataRow dr;
    DataRow dr1;
dt.Columns.Add("Name");
        dt.Columns.Add("mark");
        for (int i = 0; i < 5; i++)
        {
            dr = dt.NewRow();
            dr1 = dt.NewRow();
            dr["Name"] = i;
            dr["mark"] = i;
            dt.Rows.Add(dr);
            dt.Rows.Add(dr1);
}
GridView1.DataSource = dt;
        GridView1.DataBind();


Client side code:
<div>
        <asp:Panel ID="Panel1" runat="server">
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false" CssClass ="td">
        <Columns >
        <asp:BoundField  HeaderText ="Name" DataField ="Name"></asp:BoundField>
        <asp:BoundField  HeaderText ="Mark" DataField ="Mark" />
        </Columns>
        </asp:GridView>
    </asp:Panel>
    </div>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="printItn();"/>
    </div>


Finally we use java script code to print a grid view
<script type="text/javascript" >
   function printItn() {
            var printContent = document.getElementById("Panel1");
            var windowUrl = 'about:blank';
            var windowName = 'Print';
            var WinPrint = window.open(windowUrl, windowName, 'left=300,top=300,right=500,bottom=500,width=1000,height=500');
            WinPrint.document.write('<' + 'html' + '><head></head><' + 'body style="background:none !important"' + '>');
            WinPrint.document.write(printContent.innerHTML);
            WinPrint.document.write('</body></html>');
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
            WinPrint.close();
        }
   </script>





So when click the print button the new page open with grid view data also with print screen..
I hope you like this method to print only grid view in a webpage.

0 comments:

Post a Comment