Tuesday 25 December 2012

print the data from gridview only

This article will discuss how you can use our PrintLib.Net and ActivePdf server to create PDF reports of the data displayed in your grid. The article can help answer some frequently asked questions like:

How to print GridView?
How to print DataGrid?
How to create PDF report from a DataSet?
How to print ASP.Net page as PDF document?
Create PDF report from ASP.Net page
... and more
A very common method used by lot of web developers for printing pages or data rendered on the pages is by asking user to hit "Print" button on browser. And almost all users do not like the print outs because they contain menus, page footers, headers and other junk that user does not really care about. The user wants a nice looking report printed for them. Grid is most commonly used way to display tabular data. And you want to print the records in nice format and ability to have grid header on each page when data spans more than one page. To solve these very problems we created a printing library that allows us to specify layout for the report or grid and then use a print driver to render the report and then stream is back to client.

We have used ActivePDF server as print driver and PrintLib.Net for specifying layput of report. You can download these 2 libraries from the following links. ActivePDF offers evaluation copy of their product to test. And as usual PrintLib.Net has a trial edition which you can use as long as you desire (with limited capabilities).

PrintLib.Net
ActivePDF Server V3.5.2 SP7
For our demo project we have setup a grid view with following columns.

<asp:GridView ID="ctlGridView" runat="server" autogeneratecolumns=False gridlines=Horizontal>
    <columns>
        <asp:boundfield datafield="ProductID" sortexpression="ProductID" />
        <asp:boundfield datafield="Name" headertext="Name" sortexpression="Name" />
        <asp:boundfield datafield="ProductNumber" headertext="Product Number" />
        <asp:BoundField DataField="ListPrice" HeaderText="List Price" DataFormatString="{0:c}" />
        <asp:BoundField DataField="SellStartDate" HeaderText="Sell Start Date" DataFormatString="{0:d}" />
    </columns>
</asp:GridView>

The following code is from the event handler that fires when user clicks on "Print" button on grid view.

Handling Print Button Click
protected void OnPrint(object sender, EventArgs e)
{
    m_obPrtMgr = new PrintManager();

    PRT2DISKLib.Print2DiskClass pdfServer = new PRT2DISKLib.Print2DiskClass();
    String strPdfFile = Server.MapPath("~/PdfReports");
    pdfServer.PDFAuthor = "Winista";
    pdfServer.OutputDirectory = strPdfFile;
    pdfServer.NewDocumentName = Guid.NewGuid().ToString("N") + ".pdf";
    strPdfFile = System.IO.Path.Combine(strPdfFile, pdfServer.DocumentName);
    pdfServer.GeneralFlags += 16;

    PrintDocument doc = new PrintDocument();
    doc.PrintPage += new PrintPageEventHandler(this.Print);
    pdfServer.PrintQuality = -4; // High resolution
    int iRetVal = pdfServer.StartPrinting();
    doc.PrinterSettings.PrinterName = pdfServer.NewPrinterName;
    SetupTextReport(m_obPrtMgr);
    doc.Print();
    pdfServer.Wait(1);
    pdfServer.StopPrinting();
    strPdfFile = "/PdrRefports/" + pdfServer.DocumentName;
    pdfServer = null;
    RegisterScriptForReportDisplay(strPdfFile);
}

Setting up grid printing layout
Following code from demo project show how we used PrintLib.Net library to set the layout for printing the data from grid view.

private PrintDataTable PrepareDataTable(DataTable dtable)
{
    PrintDataTable dt = new PrintDataTable();
    dt.DrawTableHeader = true;
    dt.DataSource = dtable.DefaultView;

    // Add column information.
    PrintDataColumnCollection cols = dt.Columns;

    PrintDataColumn col = new PrintDataColumn("ProductID", 1.25f, StringAlignment.Far);
    col.HeaderBackColor = Color.Gainsboro;
    col.BackColor = Color.Aquamarine;
    col.HeaderText = "Product ID";
    col.HeaderAlignment = StringAlignment.Center;
    cols.Add(col);

    col = new PrintDataColumn("Name", 1.25f, StringAlignment.Near);
    col.HeaderBackColor = Color.Gainsboro;
    col.ForeColor = Color.Blue;
    col.HeaderText = "Product Name";
    col.HeaderAlignment = StringAlignment.Center;
    cols.Add(col);

    col = new PrintDataColumn("ListPrice", 1.25f, StringAlignment.Far);
    col.HeaderText = "List Price";
    col.HeaderBackColor = Color.Gainsboro;
    col.HeaderAlignment = StringAlignment.Center;
    col.FormatDataColumn += new FormatDataColumnEventHandler(this.FormatDateColumn);
    cols.Add(col);

    return dt;
}

Output
You can click on the following link to download the PDF file that was generated by the demo project using trial version of PrintLib.Net and ActivePDF server.



or
are you want java-script

please try this way

<script language="javascript" type="text/javascript">
function Print(str)
{
var prntData=document.getElementById(str);
var prntWindow=window.open("","Print","width=20,height=20,left=0,top=0,toolbar=0,scrollbar=0,status=0");
prntWindow.document.write(prntData.InnerHTML);
prntWindow.document.close();
prntWindow.focus();
prntWindow.print();
prntWindow.close();
}

</script>


<div id="grid">
<asp:gridview runat="server" .......="" xmlns:asp="#unknown">
......
</asp:gridview>
</div>

 <asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="Print('grid');" />

No comments:

Post a Comment

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...