Print Invoice with Details from Datagridview (ASP.NET C# and SQL)

Last Reply 5 days ago By arunkurmi

Posted 7 days ago

Dear Friends/Sir,

I am using ASP.NET, C# and SQL

 

In my project everything is working fine viewing Values from Database in Gridview, Add, Update Delete, Convert DatagridView Values to Excel and pdf.

But now i want to Print Datagridview values with Company Details like company name, Contact Details, Address etc as header like in Bills/Invoices. 

The attached file is just a sample how i want my out put it is from google, just for referenceSample for Reference

You are viewing reply posted by: arunkurmi 5 days ago.
Posted 5 days ago Modified on 5 days ago

Hi mdkh,

Check this sample. now take its reference.

You can design your own table structure for print as you need.

HTML

<div id="dvPrint" runat="server">
    <table>
        <tr>
            <td colspan="2"><center><b><u>Order Details</u></b></center></td>
        </tr>
        <tr>
            <td>Company Name : <u><b>AspSnippets.com</b></u></td>
            <td>Date : <u><asp:Label ID="lblDate" Text="" runat="server" /></u></td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="OrderID" HeaderText="Order ID" />
                        <asp:BoundField DataField="OrderDate" HeaderText="Order Date" DataFormatString="{0:dd/mm/yyyy}" />
                        <asp:BoundField DataField="CustomerID" HeaderText="Customer Name" />
                        <asp:BoundField DataField="Freight" HeaderText="Price" DataFormatString="{0:N2}" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    <asp:Button ID="btnPrint" Text="Print" runat="server" OnClick="OnPrint" />
</div>

Namespaces

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        lblDate.Text = DateTime.Now.ToString();
        this.BindOrders();
    }
}


public override void VerifyRenderingInServerForm(Control control)
{
    /*Verifies that the control is rendered */
}

protected void OnPrint(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
    {
        dvPrint.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10, 0f);
        PdfWriter wrt = PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
        pdfdoc.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(wrt, pdfdoc, sr);
        pdfdoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=OrderDetails.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfdoc);
        Response.End();
    }
}

private void BindOrders()
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 OrderID,OrderDate,CustomerID,Freight FROM Orders", con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                sda.SelectCommand = cmd;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                this.gvOrders.DataSource = dt;
                this.gvOrders.DataBind();
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        lblDate.Text = DateTime.Now.ToString()
        Me.BindOrders()
    End If
End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Protected Sub OnPrint(ByVal sender As Object, ByVal e As EventArgs)
    Using sw As StringWriter = New StringWriter()
        Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
            dvPrint.RenderControl(hw)
            Dim sr As StringReader = New StringReader(sw.ToString())
            Dim pdfdoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10, 0.0F)
            Dim wrt As PdfWriter = PdfWriter.GetInstance(pdfdoc, Response.OutputStream)
            pdfdoc.Open()
            XMLWorkerHelper.GetInstance().ParseXHtml(wrt, pdfdoc, sr)
            pdfdoc.Close()
            Response.ContentType = "application/pdf"
            Response.AddHeader("content-disposition", "attachment;filename=OrderDetails.pdf")
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.Write(pdfdoc)
            Response.[End]()
        End Using
    End Using
End Sub

Private Sub BindOrders()
    Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
        Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 OrderID,OrderDate,CustomerID,Freight FROM Orders", con)
            cmd.CommandType = CommandType.Text
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                sda.SelectCommand = cmd
                Dim dt As DataTable = New DataTable()
                sda.Fill(dt)
                Me.gvOrders.DataSource = dt
                Me.gvOrders.DataBind()
            End Using
        End Using
    End Using
End Sub

Screenshot