Link to home
Start Free TrialLog in
Avatar of suruchijain
suruchijain

asked on

How to take a print of a grid view (with paging enabled) on button click ?

I have a gridview on which there is paging enabled (in my case 10 records per page).There is a button on the same page which when clicked should print those 10 records (on the grid.)
How should we be doing this ?
Here is the design of my grid:

<asp:GridView ID="grdvDetails" AutoGenerateColumns="true" runat="server"
                          AllowPaging="True" AllowSorting="true">
                                <RowStyle CssClass="gridContent" />
                                <HeaderStyle CssClass="gridHeader" />
                                <PagerStyle CssClass="content01" />                              
                          </asp:GridView>

and the button on which the print command should be fired:
<asp:ImageButton ID="imgbtnPrintSummary" runat="server" ImageUrl="~/images/button_print_grid.gif"
                                   width="105" height="20" OnClick="imgbtnPrintSummary_Click"  />

Thanks in advance for any help
Cheers !
ASKER CERTIFIED SOLUTION
Avatar of hamidovt
hamidovt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hamidovt
hamidovt

if you want to print only grid part you can play little bit with styles...
use something like this:
<link rel="stylesheet"   type="text/css"  media="print" href="print.css" />
or

@media print
{
    BODY { font-size: 10pt }
}
 @media screen
{
    BODY { font-size: 12pt }
}
Avatar of suruchijain

ASKER

Hi hamidovt:,

Thanks for your reply
I have found the exact solution for my query through a friend's help: her it is:

<script language="javascript" type="text/javascript">
function CallPrint(strid)
{
 var prtContent = document.getElementById(strid);
 var WinPrint = window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
 WinPrint.document.write(prtContent.innerHTML);
 WinPrint.document.close();
 WinPrint.focus();
 WinPrint.print();
 WinPrint.close();
// prtContent.innerHTML=strOldOne;
}
</script>
<!--for the grid: --> enclose in the <div/>  tag
  <div id="divPrint">
                            <asp:GridView ID="grdvSummary" runat="server" AllowPaging="True" AllowSorting="True"
                                OnPageIndexChanged="grdvSummary_PageIndexChanged" PageIndex="0" OnPageIndexChanging="grdvSummary_PageIndexChanging">                                
                            </asp:GridView>
                            </div>
the button from which we have fire the command:
<asp:ImageButton ID="imgbtnPrintSummary" runat="server" ImageUrl="~/images/button_print_grid.gif"
                                   width="105" height="20" OnClientClick="javascript:CallPrint('divPrint');"/>
: this is a lttlel similar to your 1st solution, but I wanted a print of only the grid.
Thanks again for ur help

You can do what ever you want, but I dont like solutions with so many javascript. Probably you did not understand waht I mean. I have not sent you 2 solutions, I the second post was just an additional explanation about printing only grid part. I was assuming your know stylesheets ....

@media says on which media  stylesheet will be applied. So if your define following style and mark the sections you do not want to be printed. The main advantage of that is that also "Print" from the file menu will print only the grid part and the "Print Preview" will display only the part that will be actually printed.

@media print
{
    .NONprintable { display: none;}
}

This is very handy for marking such parts as navigation and header as non printable...

As for printing part,  window.print(); will print the page. So you can add it where ever you want...

Hi,

If this solves your problem, could you please accept my solution??

I object closing the solution with points refund. I have made suggestion that would solve the problem and even after have come short on other issues (outside of the original question)  I have given additional explanation an provided solution.

I request my first comment to be accepted as a solution.


Force accepted.
kb
Experts Exchange Moderator