Dear All,
Base on the following article:
How to generate and download PDF Report from database in ASP.Net using iTextSharp C# and VB.Net
I have a Nested GridView implemented from the following article:
private void BindBestillingDropDown()
{
ddlEmployees.DataSource = GetData("SELECT DISTINCT kart_Order.OrderID, kart_Order.BestillingID, kart_Order.BestillingKartType1, kart_Order.BestillingKartNummer1, kart_Order.BestillingAntallKart1 FROM kart_Bestilling INNER JOIN kart_Order ON kart_Bestilling.BestillingID = kart_Order.BestillingID");
ddlEmployees.DataTextField = "OrderID";
ddlEmployees.DataValueField = "OrderID";
ddlEmployees.DataBind();
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void GenerateReport(object sender, EventArgs e)
{
DataRow dr = GetData("SELECT DISTINCT kart_Order.OrderID, kart_Order.BestillingID, kart_Order.BestillingKartType1, kart_Order.BestillingKartNummer1, kart_Order.BestillingAntallKart1 FROM kart_Bestilling INNER JOIN kart_Order ON kart_Bestilling.BestillingID = kart_Order.BestillingID where kart_Bestilling.BestillingID = " + ddlEmployees.SelectedItem.Value).Rows[0]; ;
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
Color color = null;
document.Open();
//Header Table
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
//Bestilling Kart Type
table.AddCell(PhraseCell(new Phrase("\n " + "Kart Type:" + "\n ", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("\n " + dr["BestillingKartType1"] + "\n ", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
cell.BackgroundColor = new Color(208, 226, 241);
table.AddCell(cell);
//Type KartNummer og Antall
table.AddCell(PhraseCell(new Phrase("\n " + "Kart Nummer:" + "\n ", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("\n " + dr["BestillingKartNummer1"] + " Antall: " + dr["BestillingAntallKart1"] + "\n ", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
cell.BackgroundColor = new Color(208, 226, 241);
table.AddCell(cell);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
Thank you in advance.